我试图做出创建自定义卡的应用程序的自定义文本图像。我想在自定义背景(JPG图片)上添加一些文字。生成与Android的
是什么做的最好的方法是什么?我需要向用户显示该卡的预览之前将其发送到服务器。
由于下面的代码
我试图做出创建自定义卡的应用程序的自定义文本图像。我想在自定义背景(JPG图片)上添加一些文字。生成与Android的
是什么做的最好的方法是什么?我需要向用户显示该卡的预览之前将其发送到服务器。
由于下面的代码
使用能达到您的要求
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();
}
您在清单文件中使用下面的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
对于我的设备的路径是/sdcard
访问外部SD卡,也可以用于其他设备而异。有些设备可能有/mnt/sdcard
可能是内部的SD卡。在使用此代码之前,请检查它。
其实我为上面的代码写了一些其他的问题,从照相机捕获后需要时间戳在照片上。我为您提供了相同的解决方案,并针对您的具体要求做了一些修改。
我希望你能理解这一点。如果您对代码有任何疑问,请随时提问。
真的很好...谢谢... – Aravin 2013-10-22 15:33:55
如何使用拖放方式移动图像上添加的文本。 – Mayur 2014-05-14 04:01:28
可以相同,而不将图像保存到外部存储,只是发送到服务器可以实现吗? – user3677331 2015-06-26 09:35:42
我不确定这是最好的解决方案,但它可以帮助你。
第1步:创建一个相对布局(任何其他),并将您的图像设置为其背景。
第2步:现在添加一个宽度和高度的textview作为fill_parent或match_parent以及重力作为top | center_horizontal。
第3步:现在添加另一个按钮或任何其他布局控件,将触发用户确认。 (您应该将此控件放置在相对布局外)。
第四步:
v1.setDrawingCacheEnabled(true);//v1 is the object of your Relative layout
Bitmap bm = v1.getDrawingCache();
if (bm != null) {
//TODO:write the code for saving the image.
Toast toast = Toast.makeText(YourActivity.this, "image saved",
Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(YourActivity.this,
"No image saved.", Toast.LENGTH_LONG);
toast.show();
}
意味着你要在一个图像文件添加文本:如果用户已确认的图像,那么你可以通过下面的代码把你的相对布局的屏幕截图? – 2012-02-03 07:09:08
我有一个图像(.JPG),并希望在顶部中心添加用户的文本,并生成与文本的新JPG包含的 – Addev 2012-02-03 07:11:02
检查我的答案。 – 2012-02-03 07:36:42