1

I am developing an application where I need to generate an Image from text and store that Image to the SDCard.
Can anyone tell me a library(just like textimagegenerator for java) I need android compatible library or source which I can use for this?

Altaf
  • 5,150
  • 10
  • 39
  • 55

5 Answers5

6
TextView textView = new TextView(activity.getContext());
textView.setText("Hello World");
textView.setDrawingCacheEnabled(true);
textView.destroyDrawingCache();
textView.buildDrawingCache();
Bitmap bitmap = getTransparentBitmapCopy(textView.getDrawingCache());


private Bitmap getTransparentBitmapCopy(Bitmap source)
{
    int width =  source.getWidth();
    int height = source.getHeight();
    Bitmap copy = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    int[] pixels = new int[width * height];
    source.getPixels(pixels, 0, width, 0, 0, width, height);
    copy.setPixels(pixels, 0, width, 0, 0, width, height);
    return copy;
}
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
2

Try to do this

Got the controls.

Button b1=(Button)findViewById(R.id.button1);
EditText ed1=(EditText)findViewById(R.id.editText1);
String msg=ed1.getText().toString();

Create the bitmap, canvas, paint,and invoke drawText function:

Bitmap bitmap = Bitmap.createBitmap(300, 400, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.CYAN);
Paint paint = new Paint();
paint.setTextAlign(Align.LEFT);// 若设置为center,则文本左半部分显示不全 paint.setColor(Color.RED);
paint.setAntiAlias(true);// 消除锯齿
paint.setTextSize(20);

canvas.drawText(msg, 20, 30, paint) ;
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();

Save the image

String path = Environment.getExternalStorageDirectory() + "/abc.png";
FileOutputStream fos = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Enli
  • 209
  • 2
  • 5
  • It would be an issue for a sentence long enough to be within device screen - it would exceed screen on left or right. Thank you anyway! – Gleichmut Aug 31 '23 at 15:49
2

You can use android.graphics.Canvas.drawText() for this.

fredley
  • 32,953
  • 42
  • 145
  • 236
0

Yo can have a look at this "Android save view to jpg or png" post:

Hope this helps

Community
  • 1
  • 1
oriolpons
  • 1,883
  • 12
  • 20
0

Assuming you have a text view or an image view, you will do this:

    TextView tv = (TextView)findViewById(R.id.textview);
    Bitmap bp;
    bp = loadBitmapFromView(tv);

    ImageView iv = new ImageView(this);
    iv.setImageBitmap(bp);

try {
    OutputStream fOut = null;
    String path = "/sdcard/";
    File file = new File(path, "imagename here.jpg");
    fOut = new FileOutputStream(file);

    getImageBitmap(bp).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
    fOut.flush();    
} catch (Exception e) {
       e.printStackTrace();
}

be sure to put this in your androidmanifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Alex Kelly
  • 1,593
  • 3
  • 14
  • 19