三星Galaxy S3,S4,S5的Android相机/图片方向问题

Aym*_*ric 11 android android-camera samsung-galaxy-camera

我正在为Android API 16到21开发相机应用程序,其主要目的是拍摄肖像照片.我可以用几个设备(Nexus 4,Nexus 5,HTC ......)拍照并正确定位(意味着我的预览等于拍摄的照片尺寸和方向).

但是我已经在其他几个设备上测试了我的应用程序,其中一些设备给我带来了很多麻烦:三星Galaxy S3/S4/S5.

在这三个设备上,预览正确显示,但方法返回的图片onPictureTaken(final byte[] jpeg, Camera camera)始终是侧面的.

这是byte[] jpeg在将ImageView保存到磁盘之前从我的用户创建并在ImageView中显示的位图:

预习

这是保存在磁盘上的图像:

磁盘

正如您所看到的,图像在预览中完全拉伸,并且一旦保存在磁盘上就会错误地旋转.

这是我的CameraPreview类(我混淆了其他方法,因为它们与相机参数无关):

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
    private SurfaceHolder surfaceHolder;
    private Camera camera;

    // Removed unnecessary code

    public void surfaceCreated(SurfaceHolder holder)
    {
        camera.setPreviewDisplay(holder);
        setCameraParameters();
        camera.startPreview();
    }

    private void setCameraParameters()
    {
        Camera.Parameters parameters = camera.getParameters();
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        int rotation = windowManager.getDefaultDisplay().getRotation();
        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 rotate = (info.orientation - degrees + 360) % 360;
        parameters.setRotation(rotate);

        // Save Parameters
        camera.setDisplayOrientation(90);
        camera.setParameters(parameters);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么这条准确的代码适用于除三星以外的其他设备?

我试图在以下SO帖子上找到答案,但到目前为止没有什么可以帮助我: 这一个另一个.

编辑

实施Joey Chong的答案并没有改变任何事情:

public void onPictureTaken(final byte[] data, Camera camera)
{
    try
    {
        File pictureFile = new File(...);
        Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
        FileOutputStream fos = new FileOutputStream(pictureFile);
        realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

        int orientation = -1;
        ExifInterface exif = new ExifInterface(pictureFile.toString());
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,  ExifInterface.ORIENTATION_NORMAL);

        switch (exifOrientation)
        {
            case ExifInterface.ORIENTATION_ROTATE_270:
                orientation = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;
                break;
            case ExifInterface.ORIENTATION_NORMAL:
                orientation = 0;
                break;
            default:
                break;
        }

        fos.close();
}
Run Code Online (Sandbox Code Playgroud)

以下是我为工作设备获得的EXIF结果:

  • 方向:0

这里是S4的结果:

  • 方向:0

Joe*_*ong 5

这是因为手机仍然保存在风景中并将元数据设置为90度.您可以尝试检查exif,在放入图像视图之前旋转位图.要检查exif,请使用以下内容:

    int orientation = -1;

    ExifInterface exif = new ExifInterface(imagePath);

    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
            ExifInterface.ORIENTATION_NORMAL);

    switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            orientation = 270;

            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            orientation = 180;

            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            orientation = 90;

            break;

        case ExifInterface.ORIENTATION_NORMAL:
            orientation = 0;

            break;
        default:
            break;
    }
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,我实施了您的解决方案(请参阅编辑),但 exifOrientation 在工作和非工作设备上始终等于 0。 (2认同)