【问题标题】:On capture Image rotate拍摄时图像旋转
【发布时间】:2019-05-03 10:52:14
【问题描述】:

在设置图像时从 Moto 设备中的相机拍摄图像后,它会向左旋转 90 度。 我已经用谷歌搜索了使用 ExifInterface 但没有解决。

【问题讨论】:

标签: java android


【解决方案1】:

将此代码添加到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
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多