[英]Draw text on jpg image android
我在里面有一個jpg圖像作為字節數組。 我如何將這個字節數組轉儲到jpg並在其上寫入canavas然后將其保存在SD卡上?
歡迎任何想法。 謝謝。
使用BitmapFactory.decodeByteArray()
獲取Bitmap
,然后使用該位圖創建一個Canvas
,並在那里繪制文本。 最后使用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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.