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

时间:2017-03-10 01:27:21

标签: java android android-camera

从相机拍摄图像后,我在gridview中显示。但是当它在网格中填充时,其方向会发生变化,并通过此方向更改保存到服务器。

我找到了一些代码,这些代码有助于在没有方向的情况下填充图像,但是当保存到服务器时,它的方向仍在改变。 下面的代码有助于设置没有方向的图像:

 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;

所以我需要在网格中显示图像,其中包含拍摄方向,并且应该以相同的方向保存在我的服务器中(我可以在我的网站中看到)

1 个答案:

答案 0 :(得分:0)

您是否尝试在拍摄照片后旋转图像而不是将其保存到文件中?

获取相机显示方向:

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

然后,使用从相机显示方向获得的旋转度将字节数组解码为位图:

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

希望得到这个帮助。