将文本转换为Android上的图像文件 [英] Convert text to image file on Android
本文介绍了将文本转换为Android上的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件(.txt)中。我想将其转换为图像文件(.png或.jpg)。例如,黑色文本在白色背景。我怎样才能做到这一点编程?
I have a text document (.txt). I want to convert it to an image (.png or .jpg). For example, black text on white background. How can I do that programmatically?
推荐答案
这(未经测试)code应该让你在正确的轨道。
this (untested) code should get you on the right track.
void foo(final String text) throws IOException{
final Paint textPaint = new Paint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
final Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
final Bitmap bmp = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.RGB_565); //use ARGB_8888 for better quality
final Canvas canvas = new Canvas(bmp);
canvas.drawText(text, 0, 20f, textPaint);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
}
这篇关于将文本转换为Android上的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文