Android How to write text on a really large image (bitmap) and save it

Hello!

This is the next problem: I need to write long text on a rather large image and save it there.

Conditions:

  • Image size over 3000 x 2000
  • Dividing the image into parts is not allowed, because the text must be written on the entire image from edge to edge.
  • Added: I cannot use the scale of the bitmap image or the scale of the Matrix, since it is necessary to write text on the original image (in full size).
  • Added by:. When I show the image to the user, I use the scale of the bitmap: options.inSampleSize = 10; and no problems with lack of memory

Current issue:

When I try to load this image into a bitmap, an error occurs with a memory limit (VM budget).

Questions:

  • Like text on a large image without loading it into memory?
  • Can existing libraries do this?

Code example:

 // Step 1. Open image and load to Bitmap Bitmap bitmap = BitmapFactory.decodeFile(fileName); // runtime Exception like "VM budget" ! // or //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.big_img_1); // as in previous runtime Exception like "VM budget" ! // or //Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream); // as in previous runtime Exception like "VM budget" ! // Step 2. Write text Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(14); Canvas canvas = new Canvas(bitmap); canvas.drawText("some long long string over all image", 100, 100, paint); // Step 3. Save bitmap to file OutputStream fOut = new FileOutputStream(new File(fileName)); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); /* _________________________________________________ | | | | | | | | | IMG | | | | | | some long long string over all image | |________________________________________________| */ 
+7
source share
2 answers

This is not exactly what you are looking for, but it can be useful.

If your image is not transparent, you can reduce the amount of memory that you need to load by specifying RGB_565 for this config as follows:

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); 
+1
source

First, use the following help to download a large bitmap

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

secondly, to write to the image and save, you can use the canvas

I am doing something similar in the application. Use Canvas.

I edited a piece of my code that actually adds a couple of other images to the background and stuff too.

Meat Code:

 private static Bitmap getPoster(...) { Bitmap background = BitmapFactory.decodeResource(res, background_id) .copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(background); Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH); font = Typeface.create(font, Typeface.BOLD); Paint paint = new Paint(); paint.setTypeface(font); paint.setAntiAlias(true); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK); float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size. paint.setTextSize(fontSize); canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE)) / 2, background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px. return background; } 

Put your own version of this class and give it the image id and everything else. It returns a bitmap so that you do whatever you want (display it in image view mode, allow the user to save it and set it as a wall panel).

0
source

All Articles