照片选择器。肖像变成景观 [英] Photo picker. Portrait turns to landscape

查看:235
本文介绍了照片选择器。肖像变成景观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app-in-android\">How从画廊(SD卡)为我在Android应用程序中选择一个图片?

我已经实现了第二个答案(同下采样)。当我在肖像选择一个图像,图像将在横向模式下显示..有谁知道这是为什么?而如何解决这一问题?在此先感谢!

I have implemented the second answer (With the downsampling). When I select an image in portrait, the image will be shown in landscape mode.. Does anybody know why this is? And how to fix this? Thanks in advance!

P.S。对不起,我做了一个新的话题了这一点,但海报保护了他的话题对新手和我一样:)

P.s. Sorry I've made a new topic out of this but the poster protected his topic against newbies like me :)

推荐答案

您必须得到图片的EXIF旋转,这样并据此安排youur位图

You have to get the exif rotation of the pic, like this and arrange youur bitmap accordingly

public static int getExifRotation(String imgPath) 
{
    try 
    {
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) 
        {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) 
            {
                case ExifInterface.ORIENTATION_NORMAL:
                    return 0;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                default:
                    return 0;
            }
        } 
        else 
        {
            return 0;
        }
    }
    catch (Exception ex) 
    {
        return 0;
    }
}

得到的图片的路径

get the path of the picture

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

细做矩阵,使用了一个矩阵位图构造

Thin make a matrix and use bitmap constructor that uses matrix

Matrix matrix = new Matrix();
matrix.preRotate(90); 
// or
matrix.postRotate(90);

所以你的onActivityResult里面你应该有这样的事情

so inside your onActivityResult you should have something like this

 Uri selectedImageUri = data.getData();

                selectedImagePath = getPath(selectedImageUri);
                orientation = getExifRotation(selectedImagePath);


                Matrix matrix = new Matrix();
                matrix.postRotate(90);
               if(orientation == 90){
                   bitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                            bitmap.getWidth(), bitmap.getHeight(), 
                            matrix, true);}

请确保您重新取样的图像,虽然第一,所以他是如何在他的答案,然后再做到这一点。

make sure you resample your image first though, so how he has it in his answer first and then do this

这篇关于照片选择器。肖像变成景观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆