在android中的文本到图像

时间:2012-01-12 17:12:02

标签: android

我正在开发一个应用程序,我需要从文本生成图像并将该图像存储到SD卡 任何人都可以告诉我一个库(就像java的textimagegenerator)我需要Android兼容的库或源代码,我可以用它吗?

5 个答案:

答案 0 :(得分:5)

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;
}

答案 1 :(得分:1)

尝试这样做

得到了控件。

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

创建位图,画布,绘画和调用drawText函数:

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();

保存图片

String path = Environment.getExternalStorageDirectory() + "/abc.png";
FileOutputStream fos = new FileOutputStream(new File(path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();

答案 2 :(得分:0)

答案 3 :(得分:0)

哟可以查看this "Android save view to jpg or png" post

希望这有帮助

答案 4 :(得分:0)

假设您有文本视图或图像视图,您将执行以下操作:

    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();
}

一定要把它放在你的androidmanifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />