0

我有一个图像视图,我想在其中显示来自手机 URI 的图片

我想根据拍摄的方向旋转图片的位图 - 但它不能始终以相同的方向显示图片。

这是我的图像视图:

<!-- picture imageView -->

<ImageView
    android:id="@+id/fragment_new_picture_imageview_picture"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/app_name"
    android:scaleType="fitXY" />

这是我的矩阵函数:

/**
 * Get the new picture {@link Bitmap} according to picture's right orientation 
 * @return the picture's {@link Bitmap}
 */
public Bitmap getPictureBitmap()
{
    //if there is a saved instance - return it
    if (pictureBitmap != null)
        return pictureBitmap;
    //decode bitmap
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    //rotate bitmap
    Matrix matrix = new Matrix();               
    matrix.postRotate(orientation);
    //create new rotated bitmap
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);     
    return bitmap;      
}

这就是清单中提到我的活动的方式:

<activity
    android:name="com.coapps.pico.background.BackgroundActivity"
    android:screenOrientation="portrait"
    android:theme="@style/MainActivityTheme" >
</activity>

使用查询 Media.EXTERNAL_CONTENT_URI 并获取 Media_ORIENTATION 字段来给出方向

4

1 回答 1

0

首先得到方向

public static int getExifOrientation(String filepath) {
                int degree = 0;
                ExifInterface exif = null;
                try {
                        exif = new ExifInterface(filepath);
                } catch (IOException ex) {
                        ex.printStackTrace();
                }
                if (exif != null) {
                        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
                        if (orientation != -1) {
                                // We only recognise a subset of orientation tag values.
                                switch (orientation) {
                                case ExifInterface.ORIENTATION_ROTATE_90:
                                        degree = 90;
                                        break;
                                case ExifInterface.ORIENTATION_ROTATE_180:
                                        degree = 180;
                                        break;
                                case ExifInterface.ORIENTATION_ROTATE_270:
                                        degree = 270;
                                        break;
                                }

                        }
                }

                return degree;
        }

然后在你的代码中使用这个 Degree 来改变 Rotation

Matrix matrix = new Matrix();               
    matrix.postRotate(orientation);// get from getExifOrientation()
    //create new rotated bitmap
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
于 2013-11-09T10:57:32.950 回答