在纵向模式下拍照时旋转的图像

时间:2013-09-27 08:13:47

标签: android camera uiimageorientation

我已经实现了一个自定义相机,我从中拍摄照片,保存后将其插入媒体商店并立即显示。我一直受到保存图像方向问题的困扰,我试图使用ExifInterface直接使用filePath或使用Android图像内容提供商的方向。

方向始终返回为0.我已经使用过:

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

编辑:

当我在横向模式下拍摄照片时,输出图像已正确显示,但在纵向模式下拍摄照片时,它会返回旋转的图像(90度)。 我目前正在使用基于EXIF的方法。

1 个答案:

答案 0 :(得分:1)

确保您传入的路径名在其开头没有“file://”。如果您的路径以前缀为前缀,ExifInterface不会抛出错误或任何内容,它每次都会返回方向的默认值。