【问题标题】:android image rotates when picking from gallery on some device like LG g4在 LG g4 等设备上从图库中挑选时,android 图像会旋转
【发布时间】:2016-01-06 09:08:24
【问题描述】:

我的应用支持从图库中挑选图片,它适用于大多数设备。 到目前为止,我对 LG g4 和三星 Galaxy s6 有疑问: 从手机相机拍摄的每张图像都将旋转保存,其 exif 参数保持为 0。当我从手机图库中选择此图像时,它将返回旋转图像(90 度),但 exif 参数为 0。

    try {
        ExifInterface exif = new ExifInterface(data.getPath());
        int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        int rotationInDegrees = exifToDegrees(rotation);
        if (rotationInDegrees == 0)
            chosenImage = RotateBitmap(chosenImage, 0);
        else if (rotationInDegrees == 90)
            chosenImage = RotateBitmap(chosenImage, 90);
        else if (rotationInDegrees == 270)
            chosenImage = RotateBitmap(chosenImage, 270);
    } catch (IOException e) {
        e.printStackTrace();
    }

exifToDegrees(旋转):

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

rotateBitmap(位图):

public static Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

【问题讨论】:

  • 现在有同样的问题。你解决了吗?

标签: android image bitmap rotation exif


【解决方案1】:

我使用 glide 库来处理这个问题。

Glide.with(this).load("your image uri").asBitmap().toBytes().fitCenter().into(new SimpleTarget<byte[]>() {
      @Override
      public void onResourceReady(byte[] resource, GlideAnimation<? super byte[]> glideAnimation) {
          imageEditorView.resetImage(BitmapFactory.decodeByteArray(resource, 0, resource.length));
      }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多