位图压缩不会改变位图字节大小

das*_*asd 6 android bitmap

我试图通过使用压缩方法来减少位图大小.

这是我的代码:

public Bitmap compressImage(Bitmap image) {
        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Log.i("before compress", immagex.getByteCount()+"");

        boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);

        if(compress)
            Log.i("after compress", immagex.getByteCount()+"");
        else
            Log.i("bad compress", "bad compress");

        return immagex;
    }
Run Code Online (Sandbox Code Playgroud)

当我检查我的日志时,我得到:

11-28 11:10:38.252: I/before compress(2429): 374544
11-28 11:10:38.262: I/after compress(2429): 374544
Run Code Online (Sandbox Code Playgroud)

为什么压缩不起作用?

更新:

我试过这段代码:

public Bitmap compressImage(Bitmap image) {
        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Log.i("before compress", immagex.getByteCount()+"");

        boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);

        Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+"");

        return immagex;
    }
Run Code Online (Sandbox Code Playgroud)

字节数仍然相同

11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544
Run Code Online (Sandbox Code Playgroud)

Bha*_*rma 0

使用 Bitmap.compress() 您只需指定压缩算法,并且压缩操作需要相当长的时间。如果您需要调整大小以减少图像的内存分配,那么您确实需要使用 Bitmap.Options 缩放图像,首先计算位图边界,然后将其解码到指定的大小。

我在 StackOverflow 上找到的最好的示例就是这个。 将图像加载到 Bitmap 对象时出现奇怪的内存不足问题

  • 我不想只是为了降低质量而缩放位图。 (2认同)