我有一张作为字节数组的jpg图片。如何将这个字节数组转换成jpg格式,然后在它的画布上绘制并将其保存到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.
decodeByteArray()
返回的是一个不可变(不可修改)的位图,所以你不能直接在上面绘制。
我在示例中添加了 .copy()
命令。这将创建一个相同的位图,但它是可变的。 - Jave希望这能帮到您。