为什么我的 PNG 字节不同?

Why are my PNG bytes different?

我正在尝试读取 Android 的 192x192 ic_launcher.pngmipmap-xxxhdpi 版本)的字节。

这是我正在做的事情:

Drawable drawable = ResourcesCompat.getDrawableForDensity
            (getResources(), R.mipmap.ic_launcher, 640, getTheme());

Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

byte[] pngBytes = stream.toByteArray();

Log.d("TEST", "" + pngBytes[37]);

我正在使用十六进制编辑器比较文件 app\src\main\res\mipmap-xxxhdpi\ic_launcher.png 的结果。

出于某种原因,pngBytes[37] 处的字节与实际字节不同。之前的字节是相同的。

什么可能导致这样的事情?

For some reason, the byte at pngBytes[37] is different than the actual byte.

嗯,当然。很可能会有很多差异。 compress() 不需要提供与您开始时相同的 PNG 文件,因为可以是:

  • 不同的压缩级别
  • 不同的调色板(例如,构建过程可能会对您的资源应用 pngquant 风格的技术)
  • 不同的像素格式(索引与简单 ARGB)
  • 等等