问题描述
我需要在我的Android应用程序创建.JPEG / .png文件编程。我有简单的图像(黑色背景),它需要编程写些文字就可以了。我该怎么办呢?可能吗?
I need to create .jpeg/.png file on my Android application programatically. I have simple image (black background), and it need to write some text on it programatically. How can I do it? Is it possible?
推荐答案
这是肯定可以的。
为您加载图像到一个位图对象的图像上书写文字。然后绘制位图的画布和颜料等功能。当你完成绘图,你只需输出的位图文件。
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
如果你仅仅使用一个黑色的背景,它可能是更好地为您简单地创建一个画布上空白位图,填充黑色,绘制文本,然后转储到一个位图。
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
这是在code,你将寻找把画布中的图像文件:
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
这篇关于是否有可能在Java中,Android的编程方式创建的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!