【问题标题】:How to make Bitmap compress without change the bitmap size?如何在不改变位图大小的情况下使位图压缩?
【发布时间】:2011-12-07 14:35:13
【问题描述】:

我用这个方法压缩一张图片

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

但我得到的图像比压缩操作之前小得多(在维度上)。

我的应用程序需要通过网络发送压缩图像 - 所以我想发送尽可能少的数据......但我必须保持图像的原始大小。

还有其他方法可以通过一些压缩来保持原始位图尺寸吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    你确定它更小吗?

    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
    

    【讨论】:

    • original.compress(Bitmap.CompressFormat.PNG, 100, out); 值 100 被忽略,因为格式是 png,也许这就是压缩位图返回与原始位图相同尺寸的原因
    • 我无法找到 1.0 MB 图像到 0.6MB 图像的逻辑或压缩值。即使压缩质量设置为 100,1.0 MB 图像仍会转换为 0.15-0.2 MB 左右的图像。无法理解压缩流程
    【解决方案2】:

    如果您使用的是 PNG 格式,那么它不会压缩您的图像,因为 PNG 是一种无损格式。使用 JPEG 压缩图像并使用 0 而不是 100 质量。

    质量接受 0 - 100

    0 = 最大压缩(适用于小图像的最低质量)

    100 = 最小压缩(适用于大图像的 MAX 质量)

    【讨论】:

    • PNG 不使用质量参数 - 来自文档:“提示压缩器,0-100。0 表示压缩为小尺寸,100 表示压缩为最大质量。某些格式,如 PNG这是无损的,将忽略质量设置..”
    • 我用了 100,这让图片很糟糕。你确定 100 是最好的质量吗?
    • 如果您的图片来源是png,则无法压缩。谢谢!
    • @stanleysantoso FYI 质量:提示压缩机,0-100。 0 表示压缩为小尺寸,100 表示压缩为最大质量。一些格式,比如无损的PNG,会忽略质量设置来源:developer.xamarin.com/api/member/…
    【解决方案3】:

    我是这样做的:

    从单例类中获取压缩位图

    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
    

    希望这会对你有所帮助。

    【讨论】:

    • 是的,这与 zetbaitsu/compressor 库中使用的代码相同,它会模糊一些低质量的图像
    • 非常好,我使用此代码处理我的应用程序的内存不足崩溃非常好。
    • @HirenPatel 如果你压缩一个尺寸为 2160 x 3840 的图像怎么办?压缩后的图片尺寸是一样还是缩小到 1080 x 1920?
    【解决方案4】:

    这是我用来减小具有高byteCount(基本上是像素)的图像大小的一种简短方法

    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 倍以上的缩放位图。可能不是最理想的解决方案,但它确实有效。

    【讨论】:

      【解决方案5】:

      您可以压缩成 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;
      }
      

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

      有什么问题请在下方留言

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多