【问题标题】:How to compress Bitmap using PNG format?如何使用 PNG 格式压缩位图?
【发布时间】:2017-08-28 12:25:42
【问题描述】:

我有一个自定义视图(WorldMapView)和一个可绘制的地图(3209x1287,PNG,64 位颜色,1.33M),我得到了

java.lang.RuntimeException: Canvas: 试图绘制过大 (148679388bytes) 位图。

我试着像这样压缩。

  WorldMapView worldMapView = (WorldMapView) itemView.findViewById(R.id.map_of_world);
  Bitmap bitmap = ((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.map)).getBitmap();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
  worldMapView.setImageBitmap(decoded);

但是如果您使用的是PNG格式,那么它不会压缩您的图像,因为PNG是一种无损格式,所以任何人都可以提出解决方案吗?

在此先感谢您。

【问题讨论】:

  • But if you are using PNG format then it will not compress your image because PNG is a lossless format你自己回答了。

标签: android bitmap android-bitmap


【解决方案1】:

我试着像这样压缩

您不能将Bitmap“压缩”到另一个Bitmap。图像以某些文件格式(例如 PNG、JPEG、WebP)在磁盘上进行压缩。 Bitmap总是未压缩。

如果您使用的是 PNG 格式,则它不会压缩您的图像,因为 PNG 是一种无损格式

这与您的问题无关。 PNG 是一种磁盘格式。 Bitmap 不是。无论您使用什么图像格式,从BitmapBitmap 都不会“压缩”位图。

那么有人可以提出解决方案吗?

将图像的分辨率降低 90% 左右。

或者,使用处理大图像的视图,例如this one

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 2011-02-12
    • 2015-01-05
    • 2015-12-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2011-07-05
    • 2013-12-28
    相关资源
    最近更新 更多