vamsiampolu
vamsiampolu

Reputation: 6622

Image rotated when taking a photo in portrait mode

I have implemented a custom camera from which I take a picture,save it insert it into the media store and display it immediately after.I have been plagued by the problem of the saved image orientation,I have tried to fix this using ExifInterface using the filePath directly or by using the orientation from the Android Images content provider.

The orientation is always returned as 0.I have already used:

Android image selected from gallery Orientation is always 0 : Exif TAG

   private int getExifOrientation(String pathName)
{
    //for complete info on EXIF orientation visit: http://sylvana.net/jpegcrop/exif_orientation.html
    ExifInterface exif=null;
    try {
        exif = new ExifInterface(pathName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("ImagePreviewActivity", "Exif data of the image could not be retreived");
    }
    int orientation=exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
    return orientation;
}

private int getRotation(int orientation)
{
    int rotation=0;
    switch(orientation)
    {
        case ExifInterface.ORIENTATION_ROTATE_90:
            //orientation values is 6
            rotation=90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            //orientation value is 3
            rotation=180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            //orientation value is 8
            rotation=270;
            break;

        case -1:
            Log.d("ImagePreviewActivity","Error getting orientation from Exif data.");
            break;

        case 1:
            Log.d("ImagePreviewActivity", "Image is properly oriented");
            default:
                Log.d("ImagePreviewActivity", "The value of orientation is "+orientation);
    }
    return rotation;
}

private Bitmap rotateBitmap(String pathName,int rotation)
{
    Bitmap bmp=BitmapFactory.decodeFile(pathName);
    Matrix matrix=new Matrix();
    matrix.postRotate(90);
    //start from x=0,y=0 and filter=false
    Bitmap rotatedBitmap=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,false);
    return rotatedBitmap;
}

EDIT:

The output image has been displaying correctly when I take the picture in the Landscape mode,however it returns a rotated image(90 degrees) when taking a picture in portrait mode. I am currently using the EXIF based method.

Upvotes: 1

Views: 1093

Answers (1)

tim.paetz
tim.paetz

Reputation: 2771

Make sure the pathname you are passing in does not have "file://" at the beginning of it. ExifInterface doesn't throw an error or anything if your path is prefixed with that, it just returns default for the orientation every time.

Upvotes: 1

Related Questions