gpt4 book ai didi

android - 在 jpg 图像 android 上绘制文本

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:59 24 4
gpt4 key购买 nike

我有一个 jpg 图像作为字节数组。如何将此字节数组转储为 jpg 并在其 Canvas 上写入,然后将其保存在 SD 卡上?

欢迎任何想法。谢谢。

最佳答案

使用BitmapFactory.decodeByteArray()获取 Bitmap,然后使用该 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.

关于android - 在 jpg 图像 android 上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com