生成与Android的自定义文本图像 [英] Generate a image with custom text in Android

查看:83
本文介绍了生成与Android的自定义文本图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图尽创建自定义卡的应用程序。我想补充一些文字上的自定义背景(如JPG图像)。

I'm trying to make an app for create custom cards. I'd like to add some text over a custom background (a jpg image).

什么是做的最好的方法是什么?我需要之前向用户显示所述卡的一个preVIEW其发送到服务器。

What is the best way of doing it? I'd need to show the user a preview of the card before send it to the server.

感谢

推荐答案

使用低于code来实现您的要求。

Use below code to achieve your requirement

    Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage); // the original file yourimage.jpg i added in resources
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

    String yourText = "My custom Text adding to Image";

    Canvas cs = new Canvas(dest);
    Paint tPaint = new Paint();
    tPaint.setTextSize(35);
    tPaint.setColor(Color.BLUE);
    tPaint.setStyle(Style.FILL);
    cs.drawBitmap(src, 0f, 0f, null);
    float height = tPaint.measureText("yY");
    float width = tPaint.measureText(yourText);
    float x_coord = (src.getWidth() - width)/2;
    cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
    try {
        dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg")));
        // dest is Bitmap, if you want to preview the final image, you can display it on screen also before saving
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您必须使用以下权限清单文件。

You have to use below permission in manifest file.

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

有关我的设备路径是 / SD卡来访问外部SD卡,它可以改变其他设备。有些设备可能有到/ mnt / SD卡可能是内部的SD卡。只需选中它,而使用这种code之前。

For my device the path is /sdcard to access external SD card, it may vary for other devices. Some devices may have /mnt/sdcard may be it is for internal sd cards. Just check it while before using this code.

其实我写上面code对于一些其他的问题,这需要对照片的时间标记捕捉从摄像头后。我给您提供您的具体要求稍微修改了同样的解决方案。

Actually I wrote the above code for some other question, which required time stamp on photo after captured from camera. I gave you the same solution with a little modifications for your specific requirement.

我希望你能理解这一点。如果你有关于任何疑问code随意问。

I hope you can understand this. If you have any doubts regarding code feel free to ask.

这篇关于生成与Android的自定义文本图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆