将字符串文本转换为位图

25

能否将位于EditText框中的字符串文本转换为位图?换句话说,是否有办法将字符串文本转换为位图,以便文本以图像形式显示?

以下是我的代码:

class TextToImage extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        //create String object to be converted to image
        String sampleText = "SAMPLE TEXT";
        String fileName = "Image";

        //create a File Object
        File newFile = new File("./" + fileName + ".jpeg");

        //create the font you wish to use
        Font font = new Font("Tahoma", Font.PLAIN, 11);

        //create the FontRenderContext object which helps us to measure the text
        FontRenderContext frc = new FontRenderContext(null, true, true);
    }
}
5个回答

94

您可以创建一个大小适当的位图,为该位图创建一个画布,然后将文本绘制到其中。您可以使用Paint对象测量文本以便知道位图所需的大小。您可以尝试以下代码(未经测试):

public Bitmap textAsBitmap(String text, float textSize, int textColor) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTextAlign(Paint.Align.LEFT);
    float baseline = -paint.ascent(); // ascent() is negative
    int width = (int) (paint.measureText(text) + 0.5f); // round
    int height = (int) (baseline + paint.descent() + 0.5f);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint);
    return image;
}

2
@shyam - 这些都是 Android 类,不是 AWT 类。它们在 android.graphics 包中。 - Ted Hopp
2
Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(16); paint.setAntiAlias(true); paint.setTypeface(Typeface.MONOSPACE); // 用黑色填充画布 c.drawText("shyam ji ", 10, 16, paint); - shyam
2
代码的两个修复: 1)使用文本上升绝对值(即 Math.abs(paint.ascent())) 2)将文本左对齐(即 paint.setTextAlign(Paint.Align.LEFT)) - Ruslan Yanchyshyn
@RuslanYanchyshyn - 谢谢你发现了这些问题!现在已经修复了。 - Ted Hopp
@TedHopp 当我设置 paint.setTextAlign(Paint.Align.CENTER); 时,文本被截断了。你知道为什么会发生这种情况吗?我该如何解决它? - Fahim
显示剩余10条评论

5

我修改了 @TedHopp 的答案,确保创建的图像始终是正方形的,这取决于图像在哪里显示,例如在 NavigationDrawer 图标或类似位置。文本在图像中垂直居中。

public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    // adapted from https://dev59.com/ZWoy5IYBdhLWcg3wD57G#8799344
    Paint paint = new Paint(ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTextAlign(Paint.Align.LEFT);
    float baseline = -paint.ascent(); // ascent() is negative
    int width = (int) (paint.measureText(text) + 0.0f); // round
    int height = (int) (baseline + paint.descent() + 0.0f);

    int trueWidth = width;
    if(width>height)height=width; else width=height;
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    canvas.drawText(text, width/2-trueWidth/2, baseline, paint);
    return image;
}

5
尝试这个:

试一试:

    public static Bitmap drawText(String text, int textWidth, int textSize) {
// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565);
Canvas c = new Canvas(b);

// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawPaint(paint);

// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();

return b;
}

2
String text = "Hello world!";
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(b, 0, 0, null);
TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16.0F);
StaticLayout sl= new StaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
c.translate(6, 40);
sl.draw(c);
return b

0

仅对于字符串我不知道,

但是,您将获得整个EditText的位图图像而不仅仅是字符串。

mEditText.setCursorVisible(false); 
mEditText.buildDrawingCache(); 
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache()); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接