如何在Android中拍摄照片后以垂直模式显示图像?

时间:2015-03-05 10:47:51

标签: android android-intent android-camera

Java代码

private void previewCapturedImage()
{
    try 
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        ExifInterface exif = null; 
        try 
        {
            exif = new ExifInterface(fileUri.getPath());
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        Log.i("file path",exifOrientation);
        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
        previewimage.setImageBitmap(bitmap);
    } 
    catch (NullPointerException e) {
        e.printStackTrace();
    }   
}

XML

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageview"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"/>

这里有些手机以纵向显示图像,有些手机在拍照后以横向显示。如何在所有类型的手机中以纵向显示图像。

2 个答案:

答案 0 :(得分:0)

private void previewCapturedImage() {



        try {

            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 1;
            ExifInterface exif = null;
            int orientation = 0;//Since API Level 5
            try {
                exif = new ExifInterface(fileUri.getPath());
                orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

            Log.i("file path",exifOrientation);

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());

            previewimage.setImageBitmap(bitmap);
            switch(orientation) {

                case ExifInterface.ORIENTATION_ROTATE_270:
                    Log.i("RotateBitmap","270");
                    RotateBitmap(bitmap, 270);
                    previewimage.setRotation(270);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    Log.i("RotateBitmap","90");
                    RotateBitmap(bitmap, 270);
                    previewimage.setRotation(90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    Log.i("RotateBitmap","180");
                    RotateBitmap(bitmap, 180);
                    previewimage.setRotation(180);
                    break;

                // etc.
            }







        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    public static Bitmap RotateBitmap(Bitmap source, float angle)
    {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        previewimage.setImageBitmap(source);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        //previewimage.setImageBitmap(source);
    }

答案 1 :(得分:0)

Matrix用于在Android中旋转位图

只需使用

 Matrix matrix = new Matrix();
 matrix.postRotate(angle_in_degrees);

并将matrix传递给bitmap.createBitmap构造函数。