android - 使用 Android 创建索引颜色 PNG

标签 android png image-compression

我想知道是否有人知道如何强制 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 user user  55878 Sep 10 19:00 8500_001.2B.png  <- Input
-rwxr--r-- 1 user user 245933 Sep 10 21:49 out.png          <- Output

这是因为原始文件使用的是索引颜色(PseudoClass),而不是真实颜色(DirectClass)。 [1]

$ identify 8500_001.2B.png
8500_001.2B.png PNG 1712x2200 1712x2200+0+0 8-bit PseudoClass 2c 55.9KB 0.000u 0:00.000

$ identify out.png
out.png PNG 1370x1760 1370x1760+0+0 8-bit DirectClass 246KB 0.000u 0:00.000

ImageMagick 足够智能,可以使用索引颜色和双色颜色图对原始双色图像进行编码。该文件在 Android 中打开、缩放和重新编码后不会执行此操作,而是为每个像素使用真实颜色,并导致文件大小大得多。

问题

  1. 有谁知道是否有办法强制标准 Android 库使用颜色图压缩文件?
  2. 如果没有,有谁知道是否有任何纯 Java 实现可以完成这 3 个任务(解码、缩放、编码)?

提前致谢。

[1] The PNG Specification

最佳答案

does anyone know if there are any pure-java implementations

是的,与 pngj图书馆。它是一个普通的 .jar 库。

请看这里: Create Index PNG with PNGJ 2.1.1

对于一些 Java 示例,这是一个 Java 应用程序: NeatoCode Techniques通过 Lance Nanek

关于android - 使用 Android 创建索引颜色 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12361969/

相关文章:

Android - 如何以编程方式点击 ListView 项目

image - 如何概括 JPEG 压缩中的量化矩阵?

optimization - 通过命令行的图像压缩工具

android - 无效的资源目录名称 obj\Debug\res menu.png

c++ - 苹果系统。如何从 PNG 数据创建图像?

iphone - 为什么 PNG 图像有时会根据其在 View 中的位置而变得模糊

java - Imageloader.save 到 ByteArrayOutputStream 的速度非常慢

java - Android NDK 性能优于常规 Java 代码

android - 当我尝试从用相机拍摄的照片中获取 Uri 时,我得到 null

android - 如何转换 Android 位图以包裹圆柱体并改变透视图