在jpg图像android上绘制文本

时间:2012-04-24 12:38:57

标签: android jpeg

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

欢迎任何想法。感谢。

2 个答案:

答案 0 :(得分:4)

使用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.

答案 1 :(得分:2)

  1. 使用BitmapFactory解码字节数组
    1. 创建Canvas
    2. 在其上绘制text
    3. Save您的位图到SD storage
  2. 希望这有帮助。

相关问题