形象定位 - 机器人 [英] Image Orientation - Android

查看:162
本文介绍了形象定位 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎和关闭在过去一个月左右的这个bug。每次,我认为我有固定它似乎又回来了以某种形式。

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.

这是旧的Andr​​oid的图片旋转90度的错误。我看过无数的帖子放在这里(计算器),藏汉作为试过无数方法,但似乎无法修复它。

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

我前两天实施以下code,这个工作对我都用我的手机上测试过它的图像。然而,当我的测试版试用试了一下,他的形象再次被旋转。他给我发的图片来测试,但他们对我的电话都显示正常。因此,为什么我越来越沮丧。

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

感谢你在前进

更新

我刚才看到的code以下行我登录落实建议的方法后:

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'

我不知道这是什么意思

I am not sure what this means

更新#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;
}

这篇关于形象定位 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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