【问题标题】:All vertical oriented images are strangely autorotated only on some devices所有垂直方向的图像仅在某些设备上奇怪地自动旋转
【发布时间】:2017-10-12 14:21:10
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android android-image android-bitmap android-image-capture


    【解决方案1】:

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

      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;
     }
    

    【讨论】:

    • 感谢您的回答,但我找不到使用此方法从 uri 获取文件路径并返回 FileNotFoundException 的有效方法。您对此有什么建议吗?
    • 我相信它在变成位图之前应该有路径。 scaleim(位图位图)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多