如何处理不支持EXIF方向数据的Android设备?

Bal*_*yto 6 android exif image orientation

我正在使用Android 2.2在HTC Desire上测试我的应用程序.它完全按照我喜欢的方式工作.我使用Sherlock软件包在旧设备上具有与较新版本相同的风格.

我的AVD设置为使用最新的Android,它看起来也不错.然后我把它放到三星Galaxy S2上,当我使用相机和图库图像时,它们旋转错误.它接缝三星(相机应用程序,它自己的Android)没有或它确实检查EXIF和我的图像是错误的.纵向图像以横向方式加载,横向图像以纵向方式加载.

  1. 我想我需要以某种方式检查EXIF并忽略它以便按原样加载图像?
  2. 更大的问题是 - 如何知道是否有其他设备(某些HTC,一些华为,无论如何)会出现类似的问题?我认为除了拥有4个屏幕尺寸组之外,所有Android设备的行为都相同...

TNX.

nse*_*iuk 4

没有任何代码很难判断发生了什么。

我发现的最简单的方法是读取 EXIF 信息并检查图像是否需要旋转。要了解有关 Android 上 ExifInterface 类的更多信息: http://developer.android.com/intl/es/reference/android/media/ExifInterface.html

也就是说,这里是一些示例代码:

/** An URI and a imageView */
public void setBitmap(ImageView mImageView, String imageURI){
    // Get the original bitmap dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();            
    Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options);
    float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI)));

    if(rotation!=0){
        //New rotation matrix
        Matrix matrix = new Matrix();
        matrix.preRotate(rotation);
        mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, reqHeight, reqWidth, matrix, true));
    } else {
        //No need to rotate
        mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options));
    }
}


/** Returns how much we have to rotate */
public static float rotationForImage(Context context, Uri uri) {
        try{
            if (uri.getScheme().equals("content")) {
                //From the media gallery
                String[] projection = { Images.ImageColumns.ORIENTATION };
                Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
                    if (c.moveToFirst()) {
                        return c.getInt(0);
                    }               
            } else if (uri.getScheme().equals("file")) {
                 //From a file saved by the camera
                    ExifInterface exif = new ExifInterface(uri.getPath());
                    int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
                    return rotation;
            }
            return 0;

        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
            return 0;
        }
}

/** Get rotation in degrees */
private static float exifOrientationToDegrees(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;
}
Run Code Online (Sandbox Code Playgroud)

如果出现错误,您将在rotationForImage 函数上看到“错误检查EXIF”日志。

  • 答案很好地解释了如何检查 ExifInterface 方向数据,但这似乎不是最初的问题。“您如何处理无法正确记录方向场的设备?” 似乎是问题的要点。这个答案仅解释了如何从 ExifInterface 获取方向字段,这在 android 文档中有明确解释。 (7认同)