在ImageView中旋转图像

时间:2015-02-18 09:27:27

标签: android image image-rotation

在我的应用中,图像是从相机捕获的,然后显示在ImageView我已成功完成此方法,但当我的图像显示在{{1}时旋转后的图像显示。
我要旋转图像,然后在Imageview中显示。
当我从前置摄像头点击图像时,图像会在下面的代码的帮助下正确显示

ImageView


但是当我从后面的相机点击时,图像将显示在前置摄像头的对面。 当我从不同角度点击相机中的图像时,还有一件事就是Matrix matrix = new Matrix(); matrix.postRotate(90); Bitmap rotate = Bitmap.createBitmap(scale,0,0,scale.getWidth(),scale.getHeight(),matrix,true); displayImage.setImageBitmap(rotate); 以不同的角度显示图像。

1 个答案:

答案 0 :(得分:3)

我在我的应用程序中使用以下代码。
它适合我... !!!

    File mediaFile = new File(mediaPath);
            Bitmap bitmap;
            if (mediaFile.exists()) {

                if (isImage(mediaPath)) {

                    Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
                    int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
                    Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
                    int rotate = 0;
                    try {
                        exif = new ExifInterface(mediaFile.getAbsolutePath());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_NORMAL:
                        rotate = 0;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                    }

                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    Bitmap rotateBitmap = Bitmap.createBitmap(scale, 0, 0, scale.getWidth(),
                            scale.getHeight(), matrix, true);
                    displayImage.setImageBitmap(rotateBitmap);
       }
}
public static boolean isImage(String str) {
        boolean temp = false;
        String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
        for (int i = 0; i < arr.length; i++) {
            temp = str.endsWith(arr[i]);
            if (temp) {
                break;
            }
        }
        return temp;
    }