如何像whatsapp一样对图像进行二次采样/调整大小 [英] How to subsample/resize an image like in whatsapp

查看:153
本文介绍了如何像whatsapp一样对图像进行二次采样/调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过whatsapp发送图像时,您可以在图像视图中看到正在发送的图像缩放得很好

When sending an image through whatsapp, you can see the image you are sending in an imageview scaled very very well

例如,我向朋友发送了两张图片

for example, i sent two images to my friend

  • 第一张图片的大小: 1296像素 X X 2304像素

第二张图像的大小: 1920像素 X X 1080像素

size of second image : 1920 pixels X 1080 pixels

此图片太大,因此whatsapp必须先缩放它们,然后才能在图片视图中显示给我

    被whatapp缩放后的第一张图像的
  • 大小 333像素 X 339像素

  • size of first image after being scaled by whatapp 333 pixels X 339 pixels

通过whatsapp缩放后的第二张图像的大小 333像素 X 187像素

size of second image after being scaled by whatsapp 333 pixels X 187 pixels

如您所见,宽度是相同的,只是高度有所不同.我试图弄清楚whatapp如何缩放此图像,但是我的方法为我提供了一张与whatsapp尺寸不同的图像

As you can see the width is the same only height differs. i have tried to figure out how whatapp scales this images, but my methods gives me an image with a different dimensions far from whatsapp's ones

方法1

private void resizeImage(){

Uri selectedImage = imageReturnedIntent.getData();
                      d("image url is " + selectedImage);
                      InputStream imageStream = getContentResolver().openInputStream(selectedImage);
                       BitmapFactory.decodeStream(imageStream,null, options);
                      imageHeight = options.outHeight;
                      imageWidth = options.outWidth;

                      options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
                      options.inJustDecodeBounds = false;
                      bitmap = BitmapFactory.decodeStream(imageStream,null, options);

}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth , int reqHeght){

    int height = options.outHeight;
    int width = options . outWidth;
    d("width is "+ width + " height is "+ height);
    d("required width is "+ reqWidth + " required height "+ reqHeght);
    int inSampleSize = 1;

    if(height > reqHeght || width > reqWidth){

        int heightRatio = Math.round((float)height/(float)reqHeght);
        d("height ratio is "+ heightRatio);

        int widthRatio = Math.round((float)width / (float) reqWidth);
        d("width ratio is "+widthRatio);

        inSampleSize = (heightRatio > widthRatio)? heightRatio :widthRatio;
    }
    d(" insample size is "+ inSampleSize);
    return inSampleSize;
}

使用上述方法输出第一张图片:宽度较小(小于333),高度很大(大于339.为579!)

Output for first image using the above method : Smaller(< 333) width, very big height(>339. it's 579!!)

第二种方法

private Bitmap scaleToFitWidth(Bitmap b, int width){
  float factor = width/b.getWidth(); // width is 333

  return Bitmap.createScaledBitmap(b, width,(int)(b.getHeight() * factor),true)
}

第二种方法输出的第一张图像:图像高度非常大!

问题?是否有人知道如何像whatapp一样很好地缩放图像?在所有设备上?(如果可能,我希望与whatsapp相同)

Question Does anyone knows how to scale images very well like whatapp Across all devices?(i would like to be the same as whatsapp if possible)

whatsapp图片

我的huawel电话

三星平板电脑

推荐答案

您可以签出此代码,使其与您想要的代码相同 WhatsApp喜欢图像压缩.该代码已根据我的用法进行了修改. 使用此代码将为您提供:

You can checkout this code it works same as you wanted WhatsApp Like Image Compression. This code has been modified according to my usage. Using this code will provide you :

  • 100kb 左右的小尺寸图像,而没有图像质量的影响.
  • 高像素图像将按比例缩小到 maxWidth maxHeight ,而不会降低其原始质量.
  • Low Size Images around 100kb without playing with image quality.
  • High pixel images will be scaled down to maxWidth and maxHeight without loosing its original quality.

原始文章:加载图像超快像WhatsApp

演示:

原始图片:大小-3.84Mb尺寸-3120 * 4160

Original Image : Size - 3.84Mb Dimensions - 3120*4160

压缩图像:大小-157Kb尺寸-960 * 1280

Compressed Image : Size - 157Kb Dimensions - 960*1280

您还可以制作一个自定义方形ImageView ,该类扩展了ImageView类. SquareImageView

Edit 1: You can also make a custom Square ImageView which extends ImageView Class. SquareImageView

这篇关于如何像whatsapp一样对图像进行二次采样/调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆