【发布时间】:2012-09-03 22:14:10
【问题描述】:
我想知道是否有人知道一种方法可以强制 Android 使用它的 compress 函数创建索引颜色的 PNG 文件。
例如:
InputStream FIS = ...
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
opt.inScaled = false;
Bitmap img = BitmapFactory.decodeStream(FIS, null, opt);
// Resize
float scale = 0.8f;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img = null; // Free
// Write
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/scaled/");
FileOutputStream FOS = new FileOutputStream(new File(dir, "out.png"));
scaledBitmap.compress(Bitmap.CompressFormat.PNG, 100, FOS);
scaledBitmap = null; // Free
此代码打开一个 PNG 文件,将其大小调整为 80% 并将其保存到 SD 卡中。生成的图像确实缩放到 80%,但生成的文件大小几乎是原始大小的 5 倍。
-rw-rw-r-- 1 个用户用户 55878 Sep 10 19:00 8500_001.2B.png <- 输入 -rwxr--r-- 1 个用户 user 245933 Sep 10 21:49 out.png <- 输出这是因为原始文件使用的是索引颜色(PseudoClass),而不是真彩色(DirectClass)。 [1]
$ 识别 8500_001.2B.png 8500_001.2B.png PNG 1712x2200 1712x2200+0+0 8 位伪类 2c 55.9KB 0.000u 0:00.000 $识别出.png out.png PNG 1370x1760 1370x1760+0+0 8 位 DirectClass 246KB 0.000u 0:00.000ImageMagick 足够聪明,可以使用索引颜色和双色颜色图对原始双色图像进行编码。该文件在 Android 中打开、缩放和重新编码后并没有这样做,而是为每个像素使用真彩色,从而导致文件更大。
问题
- 有谁知道是否有办法强制标准 Android 库使用颜色图压缩文件?
- 如果没有,有谁知道是否有任何纯 Java 实现可以只完成这 3 个任务(解码、缩放、编码)?
提前致谢。
【问题讨论】:
标签: android png image-compression