想在 android 中的新捕获图像上添加一些文本
want to add some text over a newly captured image in android
这是我的问题:
- 从相机拍摄图像
- 在上面写一些文字
- 将其保存到应用程序文件夹中
我已经介绍了第一点。
帮我解决剩下的两点
提前致谢
试试这个
1 - 将文本写入位图的代码
Bitmap bitmap = ... // your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("Some Text here", x, y, paint);
2 - 将位图保存到存储器的代码
// Assume block needs to be inside a Try/Catch block.
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "File.jpg");
fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
来源:Save bitmap to location
How to write text on an image
这是我的问题:
- 从相机拍摄图像
- 在上面写一些文字
- 将其保存到应用程序文件夹中
我已经介绍了第一点。 帮我解决剩下的两点
提前致谢
试试这个
1 - 将文本写入位图的代码
Bitmap bitmap = ... // your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("Some Text here", x, y, paint);
2 - 将位图保存到存储器的代码
// Assume block needs to be inside a Try/Catch block.
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "File.jpg");
fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
来源:Save bitmap to location
How to write text on an image