如何在图像上绘制文字?
我想在图像上绘制文本(用于保存带有文本的图像)。我有图像视图,我将位图设置为该图像,我想在图像上绘制文本(用户输入的文本)。我在保存之前尝试过这个......
void saveImage() {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Xml代码是..
<FrameLayout
android:id="@+id/framelayout"
android:layout_marginTop="30dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/text_view2"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:text="SampleText"
android:textSize="12pt"
android:layout_alignTop="@+id/ImageView01"
android:layout_height="wrap_content"/>
</FrameLayout>
I want to draw text on image ( for saving that image with text ). i have image view i set bitmap to that image i want to Draw the text on image (text entered by user ). i tried this before saving.....
void saveImage() {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Xml code is..
<FrameLayout
android:id="@+id/framelayout"
android:layout_marginTop="30dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/text_view2"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:text="SampleText"
android:textSize="12pt"
android:layout_alignTop="@+id/ImageView01"
android:layout_height="wrap_content"/>
</FrameLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
按照Vladislav Skoumal的建议,尝试这个方法:
调用这个方法
输出
As suggested by Vladislav Skoumal, try this method:
call this method
the output
更新了 SaveImage() 方法,以支持文本绘制。
让我知道这是否适合您。
沙什
Updated SaveImage() method, to support text drawing.
Let me know if this works for you.
Shash
您可以扩展视图来创建自定义视图。 在ondraw方法中,
您可以使用canvas.drawBitmap和canvas.drawText来绘制位图和文本。
这样您就不需要框架布局,因为所有内容都在单个自定义视图中。
您可以将其包含在您的 xml 文件中,如下所示
You can extend a view to create a custom view. Something like
In the ondraw method you can use canvas.drawBitmap and canvas.drawText to draw bitmaps and text.
This way you do not requirre a framelayout as everything is in a single custom view.
You can include this in your xml file as
伪代码:
Pseudo code:
在图像上填充文本视图。
请参阅 http://www.android10.org/index.php/forums/43-view-layout-a-resource/715-tutorial-android-xml-view-inflation 了解基本示例。
这应该是最简单的方法。
Inflate a text view over the image .
Refer http://www.android10.org/index.php/forums/43-view-layout-a-resource/715-tutorial-android-xml-view-inflation for a basic example.
This should be the easiest way.
我解决了这个问题(不可变文件),但文件中没有写入任何内容...按照我的代码:
公共静态文件writeOnImage(文件文件)抛出IOException {
I solve this problem (immutable file), but nothing is write in file... follow my code:
public static File writeOnImage(File file) throws IOException {
如果您使用 Glide 获取图像,我修改了 @Dwivedi 对此的回答(使用 kotlin):
In case you using Glide to get the image, i modified @Dwivedi's answer to this (using kotlin) :