android front and back camera captured picture orientation issue, rotated in a wrong way

本小妞迷上赌 提交于 2019-12-04 09:20:45

问题


I have a camera app in portrait mode which takes pictures from both front and back end cameras.The issue is like the captured images are rotated in a wrong way...

For preview i have used the following code....

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

But the captured images are rotated in a wrong way.i have also tried to rotate the captured image using matrix.postRotate(bitmap).That too doesn't work in some devices like nexus..I tried EXIF also.But here i have url instead of filepath.That doesn't work as well. can anyone help me ?


回答1:


You can refer below code.

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

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

And also refer this link https://stackoverflow.com/a/6124375/1441666




回答2:


You can use this to get orientation from a 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);
            }

And as note rotation '3 = 180, 6 = 90, 8 = 270'




回答3:


I'm using the following code for this:

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



回答4:


try this code snippet

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

and then rotate the matrix as per orientation you get

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



回答5:


the selected answer just gives the the possible rotation that may have been saved in EXIF header. in several cases camera doesn't set "ExifInterface.TAG_ORIENTATION" attribute in EXIFHeader so it will be 0(ExifInterface.ORIENTATION_UNDEFINED). and event if it is set it will be true in only one case/orientation when the picture is taken. you have to manually set rotation in camera parameters using setRotation() method. the documentation of setRotation() is very clear and also explains how to calculate rotation with consideration of device rotation and camera sensor orientation(usually landscape).

so check out setRotation() method . thats what you need to change.



来源:https://stackoverflow.com/questions/13062769/android-front-and-back-camera-captured-picture-orientation-issue-rotated-in-a-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!