如何像whatsapp那样对图像进行二次采样/调整大小 [英] How to subsample/resize an image like in 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 2304 像素
第二张图片的大小:1920 像素 X 1080 像素
size of second image : 1920 pixels X 1080 pixels
此图片太大,因此whatsapp 必须先缩放它们,然后才能在图像视图中显示给我
whatapp 缩放后第一张图片的大小 333 像素 X 339 像素
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
方法一
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 图片
我的华威手机
三星平板
推荐答案
您可以查看此代码,它的工作原理与您想要的相同 WhatsApp Like图像压缩.这段代码根据我的使用做了修改.使用此代码将为您提供:
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.
演示:
原始图片:尺寸 - 3.84Mb 尺寸 - 3120*4160
Original Image : Size - 3.84Mb Dimensions - 3120*4160
压缩图像:尺寸 - 157Kb 尺寸 - 960*1280
Compressed Image : Size - 157Kb Dimensions - 960*1280
编辑 1:您还可以制作一个扩展 ImageView
类的自定义方形 ImageView.SquareImageView
Edit 1: You can also make a custom Square ImageView which extends ImageView
Class. SquareImageView
这篇关于如何像whatsapp那样对图像进行二次采样/调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!