android前后摄像头捕捉到的图片方向问题,旋转方向错误

android前后摄像头捕捉到的图片方向问题,旋转方向错误,android,camera,orientation,nexus-s,Android,Camera,Orientation,Nexus S,我有一个肖像模式的相机应用程序,它可以从前端和后端相机拍摄照片。这个问题就像是拍摄的图像以错误的方式旋转 对于预览,我使用了以下代码 Camera.Parameters parameters = camera.getParameters(); android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.

我有一个肖像模式的相机应用程序,它可以从前端和后端相机拍摄照片。这个问题就像是拍摄的图像以错误的方式旋转

对于预览,我使用了以下代码

    Camera.Parameters parameters = camera.getParameters();
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(defaultCameraId, info);
        int rotation = this.getWindowManager().getDefaultDisplay()
                .getRotation();
        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {

            int degrees = 0;
            switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
            }
            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360; // compensate the mirror
            } else { // back-facing
                result = (info.orientation - degrees + 360) % 360;
            }

            camera.setDisplayOrientation(result);

        } else {
            parameters.set("orientation", "portrait");
        }

        camera.setParameters(parameters);

但是捕获的图像以错误的方式旋转。我还尝试使用
matrix.postRotate(位图)
旋转捕获的图像。这在某些设备(如nexus)中也不起作用。我也尝试了EXIF。但这里我使用了url而不是filepath。这也不起作用。有人能帮我吗?

你可以参考下面的代码

ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5

String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

也请参考此链接

我使用以下代码来实现此目的:

ExifInterface exif = new ExifInterface(getUriPath(uri));
int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
getUriPath:

public String getUriPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(projection[0]);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else
        return null;
}

您可以使用它从
Uri

                    String filePath = mImageUri.getPath();
                if (filePath != null) {
                    rotation = -1;
                        ExifInterface exif = new ExifInterface(filePath); // Since
                                                                            // API
                                                                            // Level
                                                                            // 5
                        rotation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                        // //Log.v("roation",
                        // exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                    }

                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }
请注意,旋转“3=180,6=90,8=270”

试试这个代码片段

try {

    ExifInterface exif = new ExifInterface(filePath);
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
    Log.v("log", "ort is "+orientation);

    } catch (IOException e)
    {
        e.printStackTrace();
    }
然后根据得到的方向旋转矩阵

if (orientation==6)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
else if (orientation==8)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(270);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

else if (orientation==3)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(180);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true;
}

所选答案仅给出可能已保存在EXIF标题中的可能旋转。在某些情况下,相机不会在EXIFHeader中设置“ExiFinInterface.TAG_ORIENTATION”属性,因此它将为0(ExiFinInterface.ORIENTATION_未定义)。事件如果设置,则在拍摄照片时,仅在一种情况/方向上为真。必须使用setRotation()方法在摄影机参数中手动设置旋转。setRotation()的文档非常清晰,还解释了如何在考虑设备旋转和相机传感器方向(通常是横向)的情况下计算旋转


所以请检查setRotation()方法。这就是您需要更改的内容。

我正在获取该图像的url。如何将其转换为文件名?我想您可以根据自己的需要进行调整,但不确定这是否能解决您遇到的问题。