Draw text on jpg image

I have a jpg image inside as an array of bytes. How can I dump this byte array in jpg and write canavas to it and then save it to the SD card?

Any ideas are welcome. Thank.

+3
source share
2 answers

Use BitmapFactory.decodeByteArray()to get Bitmap, then create Canvaswith this bitmap and draw text there. Finally save it using Bitmap.compress():

Bitmap bmp = BitmapFactory.decodeByteArray(myArray, 0, myArray.length).copy(Bitmap.Config.RGBA_8888, true); //myArray is the byteArray containing the image. Use copy() to create a mutable bitmap. Feel free to change the config-type. Consider doing this in two steps so you can recycle() the immutable bitmap.
Canvas canvas = new Canvas(bmp);
canvas.drawText("Hello Image", xposition, yposition, textpaint); //x/yposition is where the text will be drawn. textpaint is the Paint object to draw with.

OutputStream os = new FileOutputStream(dstfile); //dstfile is a File-object that you want to save to. You probably need to add some exception-handling here.
bmp.compress(CompressFormat.JPG, 100, os); //Output as JPG with maximum quality.
os.flush();
os.close();//Don't forget to close the stream.
+4
source

, .

+2

All Articles