【发布时间】:2014-10-29 07:29:46
【问题描述】:
我正在编写一个简单的相机应用程序,它可以让用户拍照,该应用程序会将一些信息写入图像中。
我遇到了内存不足错误,因为图像可能很大。取决于相机硬件。
这就是我将文本写入图像的方式:
public static byte[] writeTextOnImage(byte[] imgData, String text) {
Bitmap bitmap = BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
int dist = 30;
Rect areaRect = new Rect(dist, mutableBitmap.getHeight() - dist, 170, (int) (mutableBitmap.getHeight() - dist * 1.6f));
paint.setColor(Color.BLACK);
canvas.drawRect(areaRect, paint);
RectF bounds = new RectF(areaRect);
bounds.right = paint.measureText(text, 0, text.length());
bounds.bottom = paint.descent() - paint.ascent();
bounds.left += (areaRect.width() - bounds.right) / 2.0f;
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f;
paint.setColor(Color.WHITE);
canvas.drawText(text, bounds.left, bounds.top - paint.ascent(), paint);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
我想我什至不应该考虑加载整个图像,但我仍然必须以某种方式在上面写字。是否可以编辑图像文件而不将其全部加载到内存中?
【问题讨论】:
-
您必须在图像上写下文本还是其他解决方案也可以?
-
我想你想要一种标签或水印......是吗?
-
是的。它必须在图像上。 exif 数据也在那里。
-
所以带有TextView的位图和屏幕截图不起作用?
-
不,我需要全分辨率图片
标签: android memory canvas bitmap out-of-memory