在我的应用程序中,当我从相机拍摄照片时,我需要获取该照片的大小并压缩它,如果它大于指定的大小。 我应该如何知道图像的大小并压缩它根据我的应用..
请帮帮我。
最佳答案
要获取高度和宽度,您可以调用:
Uri imagePath = Uri.fromFile(tempFile);//Uri from camera intent
//Bitmap representation of camera result
Bitmap realImage = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
realImage.getHeight();
realImage.getWidth();
要调整图像大小,我只需将生成的位图提供给此方法:
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min((float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height,
filter);
return newBitmap;
}
基本的实际上只是 Bitmap.createScaledBitmap()
。但是,我将它包装到另一种方法以按比例缩小它。
关于android - 如何在android中减小相机图片的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10975271/