首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android前后摄像头捕捉到图片方向问题,旋转方向错误

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

Stack Overflow用户
提问于 2012-10-25 14:24:05
回答 5查看 7.8K关注 0票数 17

我在肖像模式下有一个相机应用程序,可以从前端和后端拍摄照片,cameras.The问题就像捕获的图像被以错误的方式旋转…

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

代码语言:javascript
复制
    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).That来旋转捕获的图像。在一些设备上,如nexus..我尝试了also.But,在这里我有url,而不是filepath.That,也不能工作。有人能帮我吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-10-25 20:29:52

你可以参考下面的代码。

代码语言:javascript
复制
ExifInterface exif = new ExifInterface(SourceFileName);     //Since API Level 5

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

也可以参考这个链接https://stackoverflow.com/a/6124375/1441666

票数 7
EN

Stack Overflow用户

发布于 2012-11-05 00:34:12

您可以使用它从Uri获取方向

代码语言:javascript
复制
                    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‘

票数 4
EN

Stack Overflow用户

发布于 2012-11-04 01:49:49

为此,我使用了以下代码:

代码语言:javascript
复制
ExifInterface exif = new ExifInterface(getUriPath(uri));
int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

getUriPath:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13062769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档