【发布时间】:2014-05-21 18:58:10
【问题描述】:
我正在 Android 中编写自定义打印应用程序,并且正在寻找节省内存的方法。我需要在整页上打印三个基本矩形。目前我正在创建一个基本的Bitmap 页面大小:
_baseBitmap = Bitmap.createBitmap(width/_scale, height/_scale, Bitmap.Config.ARGB_8888);
打印进程请求该页面的Rect 部分。我无法预先确定这个 Rect 的尺寸。
newBitmap = Bitmap.createBitmap(fullPageBitmap, rect.left/_scale, rect.top/_scale, rect.width()/_scale, rect.height()/_scale);
return Bitmap.createScaledBitmap(newBitmap, rect.width(), rect.height(), true);
使用位图配置 ARGB_8888 _baseBitmap 大约是 28MB (8.5"x11" @ 300dpi = 2250*3300*4bytes)。即使在 50% 的缩放比例下(上面使用过),我的图像也超过 7MB。缩放比这个小,图像质量太差了。
我尝试使用Bitmap.Config.RGB_565 创建_baseBitmap,这确实大大减小了整个图像的大小,但是当我叠加图像(jpegs)时,我得到了有趣的结果。图像在宽度上被压缩,在其自身旁边复制,并且所有颜色都是绿色的。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap myBitmap = BitmapFactory.decodeStream(input, null, options);
input.close();
return myBitmap;
....
private static Bitmap overlay(Bitmap bmp1, Bitmap bmp2, float left, float top) {
Canvas canvas = new Canvas(bmp1);
canvas.drawBitmap(bmp2, left, top, null);
return bmp1;
}
我知道我可以将这些尺寸的图像压缩到合理的尺寸。我查看了Bitmap.compress,但由于某种超出我理解的原因,我得到了相同大小的图像:
ByteArrayOutputStream os = new ByteArrayOutputStream();
_baseBitmap.compress(Bitmap.CompressFormat.JPEG, 3, os);
byte[] array = os.toByteArray();
Bitmap newBitmap = BitmapFactory.decodeByteArray(array, 0, array.length);
_baseBitmap.getAllocationByteCount() == newBitmap.getAllocationByteCount()
创建一个压缩文件比创建一个大文件然后压缩它要好。有没有办法创建压缩位图?非常感谢任何建议。
注意:不是 Android 专家。我不一定熟悉您可能用来回应的平台特定术语。请温柔一点。
【问题讨论】:
-
compress中的第二个参数是质量范围为 1-100。我不确定你为什么使用 3。compress不会改变图像的分辨率,只是压缩大小。 -
3 - 高压缩的随机低值,不是吗?
标签: android memory-management printing bitmap compression