简体   繁体   中英

captures camera picture rotates on some android devices

On some devices (Nexus 9, Nexus S, ...) the camera loads a rotated picture in ImageView . I've tried to fix that with ExifInterface but no success. Does anybody have an idea on this code sample?

compiled SDK version is 25
min SDK version is 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);
    }
}
}

use following code:

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

get rotation of device from here:

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;
        }

Use above code to detect mobile orientation and save image as per device rotaion if you dont want to recognize mobile rotation set int orientation = your rotation;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM