1

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

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

从画廊方向选择的 Android 图像始终为 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 的方法。

4

1 回答 1

1

确保您传入的路径名的开头没有“file://”。如果您的路径带有前缀,ExifInterface 不会引发错误或任何错误,它只是每次都返回默认的方向。

于 2013-11-19T01:20:50.427 回答