【问题标题】:Bitmap compressed with quality=100 bigger file size than original以质量压缩的位图=比原始文件大 100 的文件大小
【发布时间】:2023-05-03 16:19:01
【问题描述】:

我正在尝试将图像发送到服务器。在发送之前,我会减小它的尺寸和质量,然后修复任何旋转问题。我的问题是,旋转图像后,当我保存它时,文件比以前大。旋转前大小为 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;
}

【问题讨论】:

    标签: android bitmap compression


    【解决方案1】:

    您正在使用不同的质量度量对其进行压缩。旋转后,您使用的是质量 100,因此它将比前一个文件更大,质量为 55。

    当您压缩图像时,当前文件的大小/质量是多少并不重要。这对结果没有真正的影响。以 55 质量压缩,然后以 100 质量压缩,不会生成与简单 55 质量压缩相同大小的文件。它会生成一个大小为 100 质量压缩的文件,因为这是对它做的最后一件事。


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

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

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

    【讨论】:

    • 感谢 Geobits,但我使用的是质量 100 和之前生成的文件(质量 55),而不是原始文件。或者这就是我认为我正在做的事情。以质量55保存图像后,文件大小为10092。然后,我将那个文件,旋转,并以质量100保存,现在文件大小为54226
    • 感谢 Geobits,我想我现在明白了。有什么办法可以解决旋转问题,减小其大小和质量,而最终文件不会比原始文件大吗?我尝试进行第一次旋转,然后减小尺寸和质量,但旋转时它给了我 OutOfMemoryException。有什么想法吗?