如何在不改变位图尺寸的情况下压缩位图?

74

我使用这种方法来压缩图像

if(bitmapObject.compress(Bitmap.CompressFormat.PNG, 100, fOut))
{
    ... 
}

但是我得到的图像在压缩后尺寸变小了。

我的应用程序需要通过网络发送压缩后的图像——所以我希望尽可能地减少数据量……但我必须保持图像的原始大小。

有没有其他方法可以在进行压缩的同时保持原始位图尺寸?

6个回答

114

你确定它更小吗?

Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());
给出。
12-07 17:43:36.333: E/Original   dimensions(278): 1024 768
12-07 17:43:36.333: E/Compressed dimensions(278): 1024 768
也许您从资源中获取位图,在这种情况下,位图的尺寸将取决于手机屏幕密度。
Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_1024x768)).getBitmap();
Log.e("Dimensions", bitmap.getWidth()+" "+bitmap.getHeight());

12-07 17:43:38.733: E/Dimensions(278): 768 576

6
由于格式为PNG,100的值被忽略了,这可能是压缩后的位图返回与原始位图相同尺寸的原因。原始代码为:original.compress(Bitmap.CompressFormat.PNG, 100, out); - Apurva
4
即使将图像压缩质量设置为100,也无法找到1.0 MB图像转换为0.6MB图像的逻辑或压缩值。即使将压缩质量设置为最高,1.0 MB的图像也会被转换为大约0.15-0.2 MB的图像。无法理解压缩流程。 - ajinkya gaurkar
压缩在很大程度上依赖于内容。对于一张图像有效的方法可能对另一张图像几乎没有影响。 - Mattwmaster58

66
如果您使用PNG格式,则它不会压缩您的图像,因为PNG是一种无损格式。请使用JPEG来压缩您的图像,并在质量中使用0而不是100。
质量接受0-100。
0 = MAX压缩(适用于小图像的最低质量)
100 = 最少压缩(适用于大图像的最高质量)

9
PNG 不使用质量参数,根据文档:"对压缩器的提示,范围在0-100之间。0表示为小文件大小而进行压缩,100表示为最高质量而进行压缩。一些无损格式如PNG将忽略质量设置..."。 - whyoz
10
我使用了100,但图片质量非常差。你确定100是最佳质量吗? - stanley santoso
1
如果您的图像源是PNG格式,则无法进行压缩。谢谢! - zionpi
@stanleysantoso FYI 质量:压缩提示,0-100。0表示为小尺寸压缩,100表示为最大质量压缩。一些格式,如无损的PNG,将忽略质量设置。 来源:https://developer.xamarin.com/api/member/Android.Graphics.Bitmap.Compress/p/Android.Graphics.Bitmap+CompressFormat/System.Int32/System.IO.Stream/ - Adeel
请点击以下链接查看与Android图像处理相关的更具体信息:https://developer.android.com/reference/android/graphics/Bitmap#compress(android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream) - mr5

21
我已经这样做了:
从单例类获取压缩位图
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = ImageUtils.getInstant().getCompressedBitmap("Your_Image_Path_Here");
imageView.setImageBitmap(bitmap);

ImageUtils.java:

public class ImageUtils {

    public static ImageUtils mInstant;

    public static ImageUtils getInstant(){
        if(mInstant==null){
            mInstant = new ImageUtils();
        }
        return mInstant;
    }

    public  Bitmap getCompressedBitmap(String imagePath) {
        float maxHeight = 1920.0f;
        float maxWidth = 1080.0f;
        Bitmap scaledBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;
        float imgRatio = (float) actualWidth / (float) actualHeight;
        float maxRatio = maxWidth / maxHeight;

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }

        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try {
            bmp = BitmapFactory.decodeFile(imagePath, options);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();

        }
        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
            } else if (orientation == 3) {
                matrix.postRotate(180);
            } else if (orientation == 8) {
                matrix.postRotate(270);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);

        byte[] byteArray = out.toByteArray();

        Bitmap updatedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        return updatedBitmap;
    }

    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        final float totalPixels = width * height;
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
        return inSampleSize;
    }
}

在压缩位图后,其尺寸保持不变。

我是如何检查的?

Bitmap beforeBitmap = BitmapFactory.decodeFile("Your_Image_Path_Here");
Log.i("Before Compress Dimension", beforeBitmap.getWidth()+"-"+beforeBitmap.getHeight());

Bitmap afterBitmap = ImageUtils.getInstant().getCompressedBitmap("Your_Image_Path_Here");
Log.i("After Compress Dimension", afterBitmap.getWidth() + "-" + afterBitmap.getHeight());

输出:

Before Compress : Dimension: 1080-1452
After Compress : Dimension: 1080-1452

希望这可以帮到你。


1
是的,这段代码与zetbaitsu/compressor库中使用的代码相同,它可以模糊一些低质量的图像。 - Harshil Pansare
1
非常好,我使用这段代码解决了我的应用程序内存不足崩溃问题,真的很棒。 - Najaf Ali
@HirenPatel 如果您压缩一个尺寸为2160 x 3840的图像,那么压缩后的图像尺寸是否会保持不变,还是会减小到1080 x 1920? - Shahood ul Hassan

3

以下是我用来缩小高字节计数(即像素)的图像大小的简短方法:

fun resizeImage(image: Bitmap): Bitmap {

    val width = image.width
    val height = image.height

    val scaleWidth = width / 10
    val scaleHeight = height / 10

    if (image.byteCount <= 1000000)
        return image

    return Bitmap.createScaledBitmap(image, scaleWidth, scaleHeight, false)
}

此方法返回一个比传入参数Bitmap小10倍以上的缩放后的图片。可能不是最理想的解决方案,但它可行。


2
你可以将图片压缩成webp格式以获得更小的大小。首先将其压缩为webp格式,然后再转换为byteArray。在转换为byteArray之后,您可以按照以下方式将其转换为位图。
public Bitmap compress(Bitmap yourBitmap){
    //converted into webp into lowest quality
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    yourBitmap.compress(Bitmap.CompressFormat.WEBP,0,stream);//0=lowest, 100=highest quality
    byte[] byteArray = stream.toByteArray();
    
    
    //convert your byteArray into bitmap
    Bitmap yourCompressBitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
    return yourCompressBitmap;
}

这一切都是在不失去图像尺寸的情况下完成的。

如果有任何问题,请在下面评论。


它比上面的resizeImage()更好,但是会有明显的质量降低。 - Claudiu Haidu
你可以增加数字来获得你想要的质量,例如在你的Bitmap.compress(Bitmap.CompressFormat.WEBP,32,stream)中。 - undefined

-8

我认为你使用这种方法来压缩位图

BitmapFactory.Option imageOpts = new BitmapFactory.Options ();
imageOpts.inSampleSize = 2;   // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, imageOpts), 96, 96, false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接