Skip to content Skip to sidebar Skip to footer

Android Front And Back Camera Captured Picture Orientation Issue, Rotated In A Wrong Way

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 hav

Solution 1:

You can refer below code.

ExifInterfaceexif=newExifInterface(SourceFileName);     //Since API Level 5StringexifOrientation= exif.getAttribute(ExifInterface.TAG_ORIENTATION);

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

Solution 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'

Solution 3:

I'm using the following code for this:

ExifInterfaceexif=newExifInterface(getUriPath(uri));
intorientation= exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

getUriPath:

publicStringgetUriPath(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;
    } elsereturnnull;
}

Solution 4:

try this code snippet

try {

    ExifInterfaceexif=newExifInterface(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)
{
    Matrixmatrix=newMatrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
elseif (orientation==8)
{
    Matrixmatrix=newMatrix();
    matrix.postRotate(270);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

elseif (orientation==3)
{
    Matrixmatrix=newMatrix();
    matrix.postRotate(180);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true;
}

Solution 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.

Post a Comment for "Android Front And Back Camera Captured Picture Orientation Issue, Rotated In A Wrong Way"