拍摄时图像旋转

时间:2019-05-03 10:52:15

标签: java android

在设置了Moto设备的相机中的图像后,图像将向左旋转90度。 我已经用Google搜索了ExifInterface,但没有解决。

1 个答案:

答案 0 :(得分:0)

将此代码添加到onActivityResult并传递图片路径

public void rotateCapturedImage(String imagePath) {
        try {
            Bitmap sourceBitmap = BitmapFactory.decodeFile(imagePath);
            sourceBitmap = getScaledBitmap(sourceBitmap, MediaPickerActivity.this);
            ExifInterface ei = new ExifInterface(imagePath);
            Bitmap bitmap = null;
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
//          Toast.makeText(mContext,"orientation "+orientation,Toast.LENGTH_SHORT).show();
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    bitmap = RotateBitmap(sourceBitmap, 90);
                    if (bitmap != null)
                        saveBitmap(bitmap, new File(imagePath));
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    bitmap = RotateBitmap(sourceBitmap, 180);
                    if (bitmap != null)
                        saveBitmap(bitmap, new File(imagePath));
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    bitmap = RotateBitmap(sourceBitmap, 270);
                    if (bitmap != null)
                        saveBitmap(bitmap, new File(imagePath));
                    break;

                default:
                    saveBitmap(sourceBitmap, new File(imagePath));
                    break;

            }
            sourceBitmap.recycle();
            if (bitmap != null)
                bitmap.recycle();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            // null value
        } catch (OutOfMemoryError e) {
            // null value
        }
    }