捕获相机图片在某些Android设备上旋转

时间:2017-08-09 12:27:24

标签: java android image camera

在某些设备(Nexus 9,Nexus S,...)上,相机会在ImageView中加载旋转的图片。我试图用 ExifInterface 解决这个问题,但没有成功。有没有人对这个代码示例有所了解?

编译的SDK版本是25
min SDK版本是15

public void capturePhoto(String targetFilename) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用以下代码:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
int rotation = getPhotoRotation();
        rotatePicture(rotation, data);
        savePicture();
        setSafeToTakePhoto(true);
    }

从这里获取设备的旋转:

private int getPhotoRotation() {
        int rotation;
        int orientation = mOrientationListener.getRememberedNormalOrientation();
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(mCameraID, info);

        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - orientation + 360) % 360;
        } else {
            rotation = (info.orientation + orientation) % 360;
        }

        return rotation;
    }
     private int getRememberedNormalOrientation() {
            rememberOrientation();
            return mRememberedNormalOrientation;
        }
     private void rememberOrientation() {
            mRememberedNormalOrientation = mCurrentNormalizedOrientation;
        }

如果您不想识别移动旋转设置int orientation =您的旋转,请使用上面的代码检测移动方向并按照设备旋转保存图像;