如何 subsample/resize 像 whatsapp 中的图片

How to subsample/resize an image like in whatsapp

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

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

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

如您所见,宽度相同,只有高度不同。我试图弄清楚 whatapp 如何缩放这些图像,但我的方法 给了我一张与 whatsapp 的图像尺寸不同的图像

方法一

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

第二种方法

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

第2种方法输出第一张图片:图片高度很大很大!

问题有谁知道如何像whatapp一样很好地缩放图像跨所有设备?(我想和如果可能的话 whatsapp)

编辑:whatsapp 图片

我的华为phone

三星平板电脑

您可以检查此代码,它与您想要的一样工作 WhatsApp Like Image Compression。此代码已根据我的用法进行了修改。 使用此代码将为您提供:

  • 大约 100kb 的小尺寸图像,不影响图像质量。
  • 高像素图像将缩小到 maxWidthmaxHeight 而不会失去其原始质量。

原创文章:Loading images Super-Fast like WhatsApp

演示:

Original Image:大小 - 3.84Mb 尺寸 - 3120*4160

Compressed Image:大小 - 157Kb 尺寸 - 960*1280

编辑 1: 您还可以创建一个 自定义 Square ImageView 扩展 ImageView Class。 SquareImageView

How to subsample/resize an image like in whatsapp

  • 您只需使用 Glide 即可,如下所示。更换 值 500,500 与您所需的图像质量。

    Glide.with(getApplicationContext())
                .load(uri)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>(500, 500) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                        bitmap = resource;//this is the required bitmap
                        imageView.setImageBitmap(resource); 
                    }
                });
    
  • 如果要将Bitmap保存为文件,使用以下方法

      private File savebitmap(String filename) {
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
      OutputStream outStream = null;
    
      File file = new File(filename + ".png");
      if (file.exists()) {
         file.delete();
         file = new File(extStorageDirectory, filename + ".png");
         Log.e("file exist", "" + file + ",Bitmap= " + filename);
      }
      try {
         // make a new bitmap from your file
         Bitmap bitmap = BitmapFactory.decodeFile(file.getName());
    
         outStream = new FileOutputStream(file);
         bitmap.compress(Bitmap.CompressFormat.JPG, 100, outStream);
         outStream.flush();
         outStream.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      Log.e("file", "" + file);
      return file;
    

    }

  • 要在您的项目中包含 Glide,请将以下内容添加到您的 gradle 文件

     repositories {
     mavenCentral() // jcenter() works as well because it pulls from Maven    Central
     }
    
     dependencies {
     compile 'com.github.bumptech.glide:glide:3.7.0'
     compile 'com.android.support:support-v4:19.1.0'
     }