图像方向 - Android [英] Image Orientation - Android

查看:19
本文介绍了图像方向 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去一个月左右的时间里,我一直在努力解决这个错误.每次我认为我已经修复它时,它似乎都会以某种形式回来.

I have been struggling with this bug on and off for the last month or so. Everytime that I think I have fixed it it seems to come back in some form.

这是旧的 Android图像旋转 90 度"错误.我在这里 (StackOverFlow) 上阅读了无数帖子,也尝试了许多方法,但似乎无法修复它.

It is the old Android "Image Rotated 90 degrees" bug. I have read countless Posts on here (StackOverFlow) aswell as tried numerous methods but just cannot seem to fix it.

我仍然收到旋转不正确的图像.

I am still getting images that are rotated incorrectly.

在我的应用程序中,用户选择他/她的个人资料图片,然后将其设置为 ImageView.图片选自手机图库

In my application a user chooses his/her profile Picture, which is then set to an ImageView. The image is chosen from the Phones Gallery

两天前我实现了以下代码,这适用于我在手机上测试的所有图像.然而,当我的一位 Beta 测试人员尝试它时,他的图像再次旋转.他给我发送了图像进行测试,但它们在我的手机上显示良好.这就是为什么我越来越沮丧.

Two days ago I implemented the Following Code, this worked for all the images I tested it with on my Phone. However when one of my Beta testers tried it, his images were once again rotated. He sent me the images for testing but they were displaying fine on my Phone. Hence why I am getting more and more frustrated.

这是我用来获取图像方向的方法:

This is the method I am using to get the Images orientation:

// Gets an Images Orientation
public static int getOrientationEXIF(Context context, Uri uri) {

    int orientation = 0;

    try {

        ExifInterface exif = new ExifInterface(uri.getPath());

        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;
                return orientation;

            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;
                return orientation;

        }

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

    return 0;
}

然后我使用这个方法得到一个旋转的位图:

I then get a rotated Bitmap using this Method:

// Rotate a Bitmap
public static Bitmap rotate(float rotationValue, String filePath) {
    Bitmap original= BitmapFactory.decodeFile(filePath);

    int width = original.getWidth();

    int height = original.getHeight();

    Matrix matrix = new Matrix();

    matrix.postRotate(rotationValue);

    Bitmap rotated = Bitmap.createBitmap(original, 0, 0, width, height, matrix, true);

    return rotated;
}

我只是不知道该做什么了.

I am just not sure what to do anymore.

如果有人能帮我解决这个问题,我真的很喜欢

I would really like it if someone could help me figure this out

提前致谢

更新

在实施建议的方法后,我刚刚在我的日志中看到了以下代码行:

I just saw the following line of Code in my Log after implementing the suggested Methods:

JHEAD can't open 'file:/external/images/media/3885'

我不确定这是什么意思

更新 #2

我想我可能已经解决了问题,我获得了文件的正确图像路径.

I think I may have fixed the problem, I got the proper image path for the file.

推荐答案

你需要考虑所有方向,而不仅仅是 90 或 180.我正在使用这个

You need to account for all orientations not just 90 or 180. I am using this

    File curFile = new File("path-to-file"); // ... This is an image file from my device.
    Bitmap rotatedBitmap;

            try {
                ExifInterface exif = new ExifInterface(curFile.getPath());
                int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                int rotationInDegrees = exifToDegrees(rotation);
                Matrix matrix = new Matrix();
                if (rotation != 0f) {matrix.preRotate(rotationInDegrees);}
                rotatedBitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);


            }catch(IOException ex){
                Log.e(TAG, "Failed to get Exif data", ex);
            }

和:

 /**
 * Gets the Amount of Degress of rotation using the exif integer to determine how much
 * we should rotate the image.
 * @param exifOrientation - the Exif data for Image Orientation
 * @return - how much to rotate in degress
 */
private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }
    return 0;
}

这篇关于图像方向 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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