android - 使用 quality=100 压缩的位图文件大小比原始文件大

标签 android bitmap compression

我正在尝试将图像发送到服务器。在发送之前,我会减小它的大小和质量,然后解决任何旋转问题。我的问题是,旋转图像后,当我保存它时,文件比以前大了。旋转前大小为10092,旋转后为54226

// Scale image to reduce it
Bitmap reducedImage = reduceImage(tempPhotoPath);

// Decrease photo quality
FileOutputStream fos = new FileOutputStream(tempPhotoFile);
reducedImage.compress(CompressFormat.JPEG, 55, fos);
fos.flush();
fos.close();

// Check and fix rotation issues
Bitmap fixed = fixRotation(tempPhotoPath);
if(fixed!=null)
{
    FileOutputStream fos2 = new FileOutputStream(tempPhotoFile);
    fixed.compress(CompressFormat.JPEG, 100, fos2);
    fos2.flush();
    fos2.close();
}

public Bitmap reduceImage(String originalPath)
{
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    o.inPurgeable = true;
    o.inInputShareable = true;
    BitmapFactory.decodeFile(originalPath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 320;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inPurgeable = true;
    o2.inInputShareable = true;
    o2.inSampleSize = scale;
    Bitmap bitmapScaled = null;
    bitmapScaled = BitmapFactory.decodeFile(originalPath, o2);

    return bitmapScaled;
}

public Bitmap fixRotation(String path)
{
    Bitmap b = null;
    try
    {
        //Find if the picture is rotated
        ExifInterface exif = new ExifInterface(path);
        int degrees = 0;
        if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6"))
            degrees = 90;
        else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8"))
            degrees = 270;
        else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3"))
            degrees = 180;

        if(degrees > 0)
        {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inPurgeable = true;
            o.inInputShareable = true;
            Bitmap bitmap = BitmapFactory.decodeFile(path, o);

            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            Matrix mtx = new Matrix();
            mtx.postRotate(degrees);

            b = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
        }
    }
    catch(Exception e){e.printStackTrace();}

    return b;
}

最佳答案

您正在使用不同的质量措施对其进行压缩。轮换后,您使用的是质量 100,因此它会比质量为 55 的前一个文件更大。

压缩图像时,当前文件的大小/质量无关紧要。这对结果没有实际影响。以 55 质量压缩,然后以 100 质量压缩,不会产生与简单的 55 质量压缩相同大小的文件。它会产生一个大小为 100 质量压缩的文件,因为这是对它所做的最后一件事。


对于您的特定代码,我不确定我是否明白将其压缩两次的原因。压缩(文件大小)不是导致旋转时出现 OOM 问题的原因,图像尺寸很可能是罪魁祸首。在旋转之前缩小图像应该可以解决这个问题,不需要保存临时文件。

您需要做的就是运行 reduceImage(),然后运行 ​​fixRotation()。修复您的旋转方法,使其接受 Bitmap 而不是路径,因此您无需在两者之间保存文件。最后,以您想要的任何质量保存/压缩它。

如果您确实出于某种原因需要临时文件,请使用 PNG 进行第一次压缩。这样它是无损的,因此当您重新压缩最终图像时,您不会以低质量使用两次 JPG(有损)。

关于android - 使用 quality=100 压缩的位图文件大小比原始文件大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15202248/

相关文章:

android - Gear VR with Unity 和 Keystore : thread priority security exception

java - 如何在android中的视频上添加应用程序 Logo

image - 如何将单 channel IplImage* 转换为位图^

java - 尝试在空对象引用上调用虚拟方法 'android.os.Bundle android.content.Intent.getExtras()'

ios - 哪些 iOS 设备不支持 ASTC 纹理压缩?

java - 处理 Lagom 服务响应的压缩

android - Android 3.0 和 4.0 中的 JIT 编译器改进

android - 如何在 TextView 中显示当前设备的 IMEI?

algorithm - gzip 文件如何存储在 HDFS 中

android - 在 MPAndroidChart 中更改 yAxis 标签的文本