25

Is it possible to convert string text that is inside an EditText box into a Bitmap? In other words, is there any way to convert string text into a Bitmap that means the text will display as an image?

Below is my Code:

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);
    }
}
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
shyam
  • 1,276
  • 4
  • 25
  • 51

5 Answers5

95

You can create a Bitmap of the appropriate size, create a Canvas for the Bitmap, and then draw your text into it. You can use a Paint object to measure the text so you'll know the size needed for the bitmap. You can do something like this (untested):

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;
}
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    @shyam - these are all Android classes, not AWT classes. They are in package `android.graphics` – Ted Hopp Jan 10 '12 at 06:25
  • hey Ted Hopp i use canvas.drawText() for drawing the text on canvas now i want to move it on touch of text means move text on finger motion..i have the code for moving image on surface of canvas ..if it is possible to make text as image then i can use it as image and move text on screen ..so how to convert text in to BitMap for move it on screen? – shyam Jan 10 '12 at 06:49
  • 2
    Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(16); paint.setAntiAlias(true); paint.setTypeface(Typeface.MONOSPACE); // fills the canvas with black c.drawText("shyam ji ", 10, 16, paint); – shyam Jan 10 '12 at 06:50
  • 2
    Two fixes to the code: 1) use text ascent absolute value (i.e. Math.abs(paint.ascent())) 2) make text left aligned (i.e. paint.setTextAlign(Paint.Align.LEFT)) – Ruslan Yanchyshyn Jul 26 '13 at 10:43
  • @RuslanYanchyshyn - Thanks for catching those! Now fixed. – Ted Hopp Jul 26 '13 at 15:26
  • @TedHopp When i set paint.setTextAlign(Paint.Align.CENTER);, the text cuts-off. do you why is it happening and how can i fix it? – Fahim Jul 04 '15 at 07:10
  • @Fahim - Without seeing your code and without any details about how it is cut off, it's hard to say. However, I suspect a coordinate system problem. When you set the text alignment to `CENTER`, you need to provide the center coordinate (not the left edge) for where you want the text when you call `drawText`, using the current coordinate system for the canvas. – Ted Hopp Jul 05 '15 at 01:44
  • Thanks, Ted, for the brilliant answer! The rectangular image was being stretched when used in certain places, so I altered the code to make the image always square (see my answer below). – Peter Gordon Oct 22 '16 at 13:38
  • I've been searching forever for a simple way to do this. I was going through the hassle of encoding strings and it wasn't working. Turns out all that was unnecessary. Plus, that way wouldn't let you mess with the Font settings. Excellent! – Peter Griffin Oct 08 '18 at 13:02
  • black background is coming, how to remove in order to get only the text as output? – Krishna Ch Jan 25 '19 at 11:53
  • @KrishnaCh - It would be best if you posted a separate question and included all the relevant code and perhaps an image of the problem as well. But perhaps your question has already been answered in this question: [How to make Canvas draw area transparent in android?](https://stackoverflow.com/q/8703581/535871). The issue there wasn't the bitmap, it was the properties of the view on which it was rendered. – Ted Hopp Jan 25 '19 at 14:58
  • @TedHopp Thanks for the response, I've solved by adding this : canvas.drawColor(context.getResources().getColor(R.color.white)); – Krishna Ch Jan 25 '19 at 15:15
  • @KrishnaCh - I'm glad you solved it. If `R.color.white` is just white, you can use the predefined constant `Color.WHITE` as the argument to `drawColor` instead of looking up a color resource value. (However, don't do this if you want the background color to change with the theme or if it might not always be white in the future.) – Ted Hopp Jan 25 '19 at 15:24
  • @TedHopp sure ill try that, and thanks for the suggestion – Krishna Ch Jan 25 '19 at 18:31
  • Perfect Solution! – Krupa Kakkad Feb 05 '20 at 10:31
5

try this :

    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;
}
ali shekari
  • 740
  • 1
  • 11
  • 26
5

I've adapted @TedHopp's answer to ensure the image created is always square, which can be useful depending on where the image is to be displayed, such as in a NavigationDrawer icon or suchlike. The text is horizontally centered in the middle of the image.

public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    // adapted from https://stackoverflow.com/a/8799344/1476989
    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;
}
Community
  • 1
  • 1
Peter Gordon
  • 1,075
  • 1
  • 18
  • 38
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
slfan
  • 8,950
  • 115
  • 65
  • 78
van
  • 21
  • 1
0

For only String I don't know but,

You will get Bitmap image of the whole EditText not only String with this,

mEditText.setCursorVisible(false); 
mEditText.buildDrawingCache(); 
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache()); 
user370305
  • 108,599
  • 23
  • 164
  • 151