将String文本转换为Bitmap

shy*_*yam 16 android image bitmap

是否可以将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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ted*_*opp 67

您可以创建适当大小的位图,为位图创建一个Canvas,然后将文本绘制到其中.您可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

  • @shyam - 这些都是Android类,而不是AWT类.它们在"android.graphics"包中 (2认同)
  • Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(16); paint.setAntiAlias(真); paint.setTypeface(Typeface.MONOSPACE); //用黑色c.drawText("shyam ji",10,16,paint)填充画布; (2认同)
  • 代码的两个修复:1)使用文本上升绝对值(即Math.abs(paint.ascent()))2)使文本左对齐(即paint.setTextAlign(Paint.Align.LEFT)) (2认同)

ali*_*ari 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;
}
Run Code Online (Sandbox Code Playgroud)


Pet*_*don 5

我已经调整了 @ TedHopp的答案,以确保创建的图像始终是 square,这可能很有用,具体取决于图像的显示位置,例如在NavigationDrawer图标等中。文本水平居中于图像中间。

public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    // adapted from /sf/answers/615954111/
    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;
}
Run Code Online (Sandbox Code Playgroud)