如何减少android中相机图片的大小

时间:2012-06-11 06:23:13

标签: android android-camera

在我的应用程序中,当我从相机拍摄照片时,我需要获得该照片的大小并压缩它,如果它超过指定的大小。 根据我的申请,我应该如何知道图像的大小并将其压缩

请帮帮我。

1 个答案:

答案 0 :(得分:1)

要获得高度和宽度,请致电:

Uri imagePath = Uri.fromFile(tempFile);//Uri from camera intent
//Bitmap representation of camera result
Bitmap realImage = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
realImage.getHeight();
realImage.getWidth();

要调整图像大小,我只需将生成的Bitmap提供给此方法:

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()。但是,我将它换成另一种方法,按比例缩小它。