Draw text on jpg image android

2020-06-27 18:50发布

问题:

I have an jpg image inside as a byte array. How can i dump this byte array to a jpg and write on it's canavas then save it on the sd card ?

Any ideas are welcome. Thanks.

回答1:

Use BitmapFactory.decodeByteArray() to get a Bitmap, then create a Canvas using that Bitmap, and draw the text there. Finally save it by 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.


回答2:

  1. Decode byte array using BitmapFactory
    1. Create a Canvas
    2. Draw text on it
    3. Save your bitmap to SD storage

Hope this helps.



标签: android jpeg