对JPG图片的android绘制文本 [英] Draw text on jpg image android

查看:175
本文介绍了对JPG图片的android绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有一个字节数组中的JPG图片。我怎么可以转储这个字节数组为JPG格式,并在其上​​写的canavas然后将其保存在SD卡上?

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.

推荐答案

使用<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cByteArray%28byte%5b%5d,%20int,%20int%29"相对=nofollow> BitmapFactory.de codeByteArray() 获得位图,然后创建一个画布使用位图,并绘制文本那里。最后保存它使用<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#com$p$pss%28android.graphics.Bitmap.Com$p$pssFormat,%20int,%20java.io.OutputStream%29"相对=nofollow> Bitmap.com preSS()

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.

这篇关于对JPG图片的android绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆