Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何像whatsapp中那样对图像进行子采样/调整大小_Android_Android Imageview_Android Image_Android Bitmap - Fatal编程技术网

Android 如何像whatsapp中那样对图像进行子采样/调整大小

Android 如何像whatsapp中那样对图像进行子采样/调整大小,android,android-imageview,android-image,android-bitmap,Android,Android Imageview,Android Image,Android Bitmap,通过whatsapp发送图像时,您可以在imageview中看到正在发送的图像,并且缩放得非常好 例如,我给我的朋友发了两张照片 第一幅图像的大小:1296像素X2304像素 第二幅图像的大小:1920像素X1080像素 此图像太大,因此whatsapp必须先对其进行缩放,然后才能在imageview中将其显示给我 按whatapp333像素缩放后的第一幅图像的大小X339像素 按whatsapp缩放后的第二幅图像大小333像素X187像素 正如你所看到的,宽度是一样的,只是高度不同。我

通过whatsapp发送图像时,您可以在imageview中看到正在发送的图像,并且缩放得非常好

例如,我给我的朋友发了两张照片

  • 第一幅图像的大小:1296像素X2304像素

  • 第二幅图像的大小:1920像素X1080像素

此图像太大,因此whatsapp必须先对其进行缩放,然后才能在imageview中将其显示给我

  • 按whatapp333像素缩放后的第一幅图像的大小X339像素

  • 按whatsapp缩放后的第二幅图像大小333像素X187像素

正如你所看到的,宽度是一样的,只是高度不同。我试图弄清楚whatapp是如何缩放这些图像的,但我的方法给了我一个与whatapp不同维度的图像

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

第二种方法

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图像

我的华威尔手机

三星平板电脑


您可以签出此代码,它的工作原理与您想要的相同。此代码已根据我的使用情况进行了修改。 使用此代码将为您提供:

  • 小于或等于100kb的低尺寸图像,无需播放图像质量
  • 高像素图像将缩小到maxWidth和maxHeight,而不会失去其原始质量
原文:

演示:

:尺寸-3.84Mb尺寸-3120*4160

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

编辑1:您还可以创建一个自定义的方形ImageView,它扩展了
ImageView

如何像whatsapp中那样对图像进行子采样/调整大小

  • 您只需使用以下命令即可完成此操作。替换 值500500,具有所需的图像质量

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

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


我还想知道,如果将因子设置为常数,例如0.5,会怎么样?@RandykaYudhistira,我认为情况会更糟。首先我认为whatsapp使用一些维度来检查图像是否大于这些维度dimensions@DanielNugent你说我的毕加索是什么意思?@RandykaYudhistira让我试试看,我会告诉你它是否管用让我试试,我会告诉你结果这里有一个主要的问题:我怎么能想出不合适的最大宽度和最大高度在一个设备中更大或在另一个设备中更小?。这是我面临的最困难的挑战whatsapp也使用与代码中相同的maxWidth和maxHeight。我知道这一点,因为我已经通过向whatsapp运行不同图像的测试用例手动测试了它。然后在
sent
文件夹中查看每个图像的大小和尺寸。当我在huawel手机中发送图像时,图像的大小调整得非常好。我要发送给的人正在使用三星平板电脑。他的手机中图像(或imageview)的大小与我手机中图像(或imageview)的大小不同。我已经编辑了我的问题。请参见照片如果使用相同的maxWidth和maxHeight,它们如何计算出将在imageview中显示给用户的大小?。
  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;
 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'
 }