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

标签 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;
    }

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

11-28 11:10:38.252: I/before compress(2429): 374544
11-28 11:10:38.262: I/after compress(2429): 374544

为什么压缩不起作用?

更新:

我试过这段代码:

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;
    }

仍然是相同的字节数

11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544

最佳答案

以下是减少位图大小并将其转换为base64的代码,

    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] byteArray = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

    Log.e("Base 64 String", imageEncoded);

关于android - 位图压缩不会改变位图字节大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261915/

相关文章:

android - QuickBlox - 来自不同位置/设备的相同用户登录

android - cocos2dx(2.0.4) android 创建项目问题

android - Android : How to get border/edge/frame when I read the background from xml? 中的自定义按钮

java - 如何在渲染 View 之前对其进行屏幕截图或快照?

c - C语言中位图的理解和获取信息

android - LinkedIn - OAuth2 - 检索所有连接列表

android - 为什么OSM中的 map 在android studio中不显示?

c++ - Fread 跳过字符读入对象

android - 如何获取使用 Volley ImageLoader 下载的缓存位图?

javascript - 如何使用 JavaScript 从 html5 canvas 检索原始位图数据?