所有垂直方向的图像仅在某些设备上奇怪地自动旋转



我有一个应用程序,可以从相机或图库中拍照,并在图像视图中显示结果。

我只通过内容提供商获取图像并使用此缩放功能

public Bitmap scaleim(Bitmap bitmap) {
       ...
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false);
        return scaledBitmap;
    }

在我的装有android 5的设备中,一切正常,现在我已经在装有Android 7的朋友设备上测试了相同的应用程序,并且每张垂直方向的图片都会自动旋转到水平方向。这看起来真的很奇怪,我不知道是什么导致了这个问题。

问题不在于缩放,但捕获的图像的工作方式因硬件而异。在开始缩放之前,应根据合适的设备进行轮换。这是以下代码:

  Matrix matrix = new Matrix();
  matrix.postRotate(getImageOrientation(url));
  Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  bitmap.getHeight(), matrix, true)
public static int getImageOrientation(String imagePath){
     int rotate = 0;
     try {
         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);
         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
    return rotate;
 }

最新更新