安卓前后摄像头拍摄的照片方向问题,以错误的方式旋转。

17

我有一个相机应用程序,它以竖屏模式从前置和后置摄像头拍照。问题是拍摄的图片旋转方向错误...

为了预览,我使用了以下代码...

    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(bitmap)来旋转捕获的图像。但在像Nexus这样的某些设备上,它也无法正常工作。我也尝试了EXIF,但这里我有一个URL而不是文件路径。那也不起作用。有人能帮帮我吗?

5个回答

7
您可以参考以下代码。
ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5

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

并且请参考这个链接:https://dev59.com/NW025IYBdhLWcg3wVkef#6124375

(注:此处没有解释,保留了HTML标签。)

1
我正在获取该图像的URL。如何将其转换为文件名? - hacker

4
您可以使用这个代码从一个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度'


我想你可以根据自己的需求进行调整,但我不确定它是否能解决你目前遇到的问题。 - Gabriel Netto

3
我将使用以下代码进行操作:

我正在使用以下代码:

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

3
请尝试这段代码片段。
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;
}

0

所选答案只是给出了可能保存在EXIF头中的旋转方向。在一些情况下,相机不会在EXIFHeader中设置“ExifInterface.TAG_ORIENTATION”属性,因此它将为0(ExifInterface.ORIENTATION_UNDEFINED)。即使设置了它,它也只在拍摄照片时的一个方向/方位上为真。您必须使用setRotation()方法手动设置相机参数中的旋转。 setRotation()的文档非常清晰,并且还解释了如何考虑设备旋转和相机传感器方向(通常为横向)计算旋转。

因此,请查看setRotation()方法。这就是您需要更改的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接