是否可以在Java,Android上以编程方式创建图像?
我需要以编程方式在我的Android应用程序上创建.jpeg/.png文件。我有简单的图像(黑色背景),它需要以编程方式在上面写一些文本。我该怎么做?可能吗?
我需要以编程方式在我的Android应用程序上创建.jpeg/.png文件。我有简单的图像(黑色背景),它需要以编程方式在上面写一些文本。我该怎么做?可能吗?
这绝对是可能的。
若要在图像上写入文本,必须将图像加载到位图对象中。然后使用“画布”和“画图”功能在该位图上绘制。完成绘制后,只需将位图输出到文件即可。
如果您只使用黑色背景,则最好只是在画布上创建一个空白位图,将其填充为黑色,绘制文本,然后转储到位图。
这是您将要查找的用于将画布转换为图像文件的代码:
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);
}