1

私のアプリケーションでは、カメラから写真を撮るときに、その写真のサイズを取得して、指定されたサイズより大きい場合は圧縮する必要があります。画像のサイズを知り、アプリケーションに応じて圧縮するにはどうすればよいですか。

私を助けてください。

4

1 に答える 1

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()です。ただし、それを別の方法にラップして、比例して縮小します。

于 2012-06-11T06:33:57.307 に答える