简体   繁体   中英

android image orientation changes from camera intent (or choosing from gallery

After taking Image from camera I'm displaying in gridview. But when its populating in grid its orientation changes and save to server with this orientation change.

I found some code which helped to populate the image without orientation, but when saves to server its orientation still changing. Below is the code helped to set the image without orientation:

 Bitmap resultBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        Bitmap returnBitmap = resultBitmap;
        try {
            ExifInterface exifInterface = new ExifInterface(filePath);   
            int orientation =                                                                         exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,        ExifInterface.ORIENTATION_UNDEFINED);
            switch (orientation) {
                default:
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    returnBitmap = rotateImage(resultBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    returnBitmap = rotateImage(resultBitmap, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    returnBitmap = rotateImage(resultBitmap, 270);
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                    break;

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return returnBitmap;

So I need to display the image in grid with orientation in which it is taken and it should save in my server (where i can see in my site) with the same orientation

Have you try rotating image after picture taken instead of save it to file?

Get camera display orientation:

    private static int getCameraDisplayOrientation(int cameraId, Activity activity) {
        int rotation = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getRotation();
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + rotation) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - rotation + 360) % 360;
        }
        return result;
    }

And then, decode byte array to bitmap using rotation degree got from camera display orientation:

public static Bitmap decodeByteArray(byte[] data, float rotationDegree) {
    try {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        if (rotationDegree != 0) {
            bm = createRotatedBitmap(bm, rotationDegree);
        }
        return  bm;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

Hope this help.

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