位图COM pressed质量= 100更大的文件大小比原来的文件大小、位图、更大、质量

2023-09-05 07:32:31 作者:嘴甜办事稳

我试图将图像发送到服务器。在发送它,我降低了它的大小和质量,然后修复任何旋转的问题。我的问题是,旋转图像后,当我保存它,该文件是大于以前。旋转尺寸之前10092后旋转54226

  //缩放图像,以减少它
位图reducedImage = reduceImage(tempPhotoPath);

//降低照片质量
FileOutputStream中FOS =新的FileOutputStream(tempPhotoFile);
reducedImage.com preSS(比较pressFormat.JPEG,55,FOS);
fos.flush();
fos.close();

//检查并修复问题,旋转
位图固定= fixRotation(tempPhotoPath);
如果(固定!= NULL)
{
    FileOutputStream中fos2 =新的FileOutputStream(tempPhotoFile);
    fixed.com preSS(比较pressFormat.JPEG,100,fos2);
    fos2.flush();
    fos2.close();
}

公共位图reduceImage(字符串originalPath)
{
    //德code图像尺寸
    BitmapFactory.Options O =新BitmapFactory.Options();
    o.inJustDe codeBounds = TRUE;
    o.inPurgeable = TRUE;
    o.inInputShareable = TRUE;
    BitmapFactory.de codeFILE(originalPath,O);

    //我们希望新的大小扩展到
    最终诠释REQUIRED_SIZE = 320;

    //找到正确的比例值。它应该是2的幂。
    INT width_tmp = o.outWidth,height_tmp = o.outHeight;
    int标= 1;
    而(真){
        如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE){
            打破;
        }
        width_tmp / = 2;
        height_tmp / = 2;
        规模* = 2;
    }

    //德code与inSampleSize
    BitmapFactory.Options O2 =新BitmapFactory.Options();
    o2.inPurgeable = TRUE;
    o2.inInputShareable = TRUE;
    o2.inSampleSize =规模;
    位图bitmapScaled = NULL;
    bitmapScaled = BitmapFactory.de codeFILE(originalPath,O2);

    返回bitmapScaled;
}

公共位图fixRotation(字符串路径)
{
    位图B = NULL;
    尝试
    {
        //找到如果图片旋转
        ExifInterface EXIF​​ =新ExifInterface(路径);
        INT度= 0;
        如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(6))
            度= 90;
        否则,如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(8))
            度= 270;
        否则如果(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase(3))
            度= 180;

        如果(度大于0)
        {
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inPurgeable = TRUE;
            o.inInputShareable = TRUE;
            点阵位图= BitmapFactory.de codeFILE(路径,O);

            INT W = bitmap.getWidth();
            INT H = bitmap.getHeight();

            矩阵MTX =新的Matrix();
            mtx.postRotate(度);

            B = Bitmap.createBitmap(位图,0,0,W,H,MTX,真);
        }
    }
    赶上(例外五){e.printStackTrace();}

    返回b;
}
 

解决方案

您是COM $ P $用不同质量的措施pssing它。旋转后,您使用的是优质的100,所以这将是比previous一个较大的文件,提供优质的55。

如何使用Filmage Screen降低视频文件大小

在COM preSS的图像,也没关系当前文件的大小/品质是什么。这对结果没有任何实际影响。 COM pressing在55质量,其次是100的质量,做的不可以结果与相同大小的文件作为一个简单的55质量COM pression。它导致的100质量COM pression大小的文件,因为这是完成它的最后一件事。

有关特定code,我不知道我看到后面的C​​OM pressing两次反正原因。 COM pression(文件大小)是不旋转的时候是什么导致你的OOM的问题,图像尺寸是最有可能的罪魁祸首。缩小图像旋转应该解决这个问题,不需要保存临时文件之前。

所有你需要做的是执行 reduceImage(),然后按照它与 fixRotation()。修复您的旋转方法,这样它接受的位图不是路径,这样你就不会需要将文件保存在两者之间。最后,保存/ COM preSS它在任何质量,您的愿望。

如果您的执行的需要临时文件由于某些原因,使用PNG的第一个COM pression。这种方式是无损的,所以当你RECOM preSS的最终图像,你会不会用JPG(有损)在低质量的两倍。

I am trying to send an image to a server. Before sending it, I am reducing its size and quality, and then fixing any rotation issue. My problem is that, after rotating the image, when I save it, the file is bigger than before. Before rotation size was 10092 and after rotation is 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;
}

解决方案

You're compressing it with different quality measures. After rotation, you're using quality 100, so it's going to be a larger file than the previous one, with quality 55.

When you compress an image, it doesn't matter what the current file size/quality is. That has no real impact on the outcome. Compressing at 55 quality, followed by 100 quality, does not result in a file with the same size as a simple 55 quality compression. It results in a file with the size of 100 quality compression, because that's the last thing done to it.

For your specific code, I'm not sure I see the reason behind compressing it twice anyway. Compression(file size) isn't what was causing your OOM problems when rotating, the image dimensions were most likely the culprit. Shrinking the image before rotating should fix that, no need for saving a temp file.

All you need to do is run reduceImage(), then follow it up with fixRotation(). Fix your rotation method so that it accepts a Bitmap instead of a path, so you don't need to save the file in between. Finally, save/compress it at whatever quality you desire.

If you do need the temp file for some reason, use PNG for the first compression. This way it's lossless, so when you recompress the final image, you won't have used JPG(lossy) twice at a low quality.