【发布时间】:2013-06-25 08:46:05
【问题描述】:
嗨!
是下面的问题:我需要在一个比较大的图片上写一个长文本并保存在那里。
条件:
- 图像尺寸超过 3000 x 2000;
- 不允许将图像分成几部分,因为要在整个图像上从边到边写入文本。
- 补充:我不能使用位图比例或矩阵比例,因为需要在原始图像上写文字(全尺寸)。
-
补充:当我向用户展示图像时,我使用位图比例:
options.inSampleSize = 10;并且没有内存不足的问题
目前的问题:
当我尝试在位图中加载此图像时,由于内存(VM 预算)的限制而发生错误。
问题:
- 如何在不将大图像加载到内存的情况下在大图像上显示文本?
- 现有的库可以做到这一点吗?
代码示例:
// Step 1. Open image and load to Bitmap
Bitmap bitmap = BitmapFactory.decodeFile(fileName); // runtime Exception like "VM budget" !
// or
//Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.big_img_1); // as in previous runtime Exception like "VM budget" !
// or
//Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream); // as in previous runtime Exception like "VM budget" !
// Step 2. Write text
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(14);
Canvas canvas = new Canvas(bitmap);
canvas.drawText("some long long string over all image", 100, 100, paint);
// Step 3. Save bitmap to file
OutputStream fOut = new FileOutputStream(new File(fileName));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
/*
_________________________________________________
| |
| |
| |
| |
| IMG |
| |
| |
| some long long string over all image |
|________________________________________________|
*/
【问题讨论】:
-
图片是否有透明部分?
-
这个有运气吗?