简体   繁体   English

android图像方向从相机意图改变(或从图库中选择)

[英]android image orientation changes from camera intent (or choosing from gallery

After taking Image from camera I'm displaying in gridview. 从相机拍摄图像后,我在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. 希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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