从图库中旋转图像位图

Rotate image bitmap from gallery

Helo 社区,

我有问题。请帮助我。

我是这样选的图:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = null;
            try {
                // Von Gallerie


                System.out.println("Test A1");

                Bitmap bitmap = null;
                    stream = getContentResolver().openInputStream(
                        data.getData());
                    bitmap = BitmapFactory.decodeStream(stream);
                stream.close();

                this.UPLOAD_URL = Config.webSiteUrl + "?action=uploadFile&username=" + this.username + "&password=" + this.password + "&baustelleid=" + Fotos.this.baustelleid;

               // System.out.println("xyy: " + this.UPLOAD_URL);

                bitmap = scaleDown(bitmap, Config.maxImageUploadSize, true);


                if(bitmap != null) {
                    uploadImage(bitmap, this.UPLOAD_URL);
                }
            } catch (IOException e1) {
              //  System.out.println("Fehler 2");
            }
        } catch (Exception e) {
         //   System.out.println("Fehler 1");
        }
}

我的问题是,某些 JPEG 图像有 EXIF-Headers,其中包含旋转。

当我显示我的图像时,问题是图像旋转了 180 度。

如何将位图旋转 180 度,以便正确显示?

(抱歉英语不好:-))

编辑:

jpeg文件的EXIF-code,周围是错误的,是:

20180712_101743.jpg: JPEG图像数据, Exif标准: [TIFF图像数据, little-endian, direntries=12, height=3096, manufacturer=samsung, model=SM-A310F, orientation=lower-right, xresolution=210, yresolution=218, resolutionunit=2, software=A310FXXU3CQL2, datetime=2018:07:12 10:17:43, width=4128], 基线, 精度 8, 4128x3096, 帧 3

其他图片,正确做法:

20180712_171712.jpg: JPEG图像数据, Exif标准: [TIFF图像数据, big-endian, direntries=12, datetime=2018:07:12 17:17:13, model=SM- A310F, resolutionunit=2, height=0, yresolution=187, orientation=[0], software=A310FXXU3CQL2, xresolution=209, manufacturer=samsung, width=0], 基线, 精度8, 4096x2606, 帧 3

检测到图像是否需要旋转后,将此状态存储在布尔变量中needsRotation并旋转或不旋转显示它的ImageView:

    ExifInterface ex = new ExifInterface(uri.getPath());
    int rotation = ex.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    Boolean needsRotation = (rotation != ExifInterface.ORIENTATION_NORMAL));
    if (needsRotation) {
        imageView.setRotation(180f);
    } else {
        imageView.setRotation(0f);
    }

一旦你有了位图,你也可以像这样创建一个旋转的副本

bitmap = scaleDown(bitmap, Config.maxImageUploadSize, true);

Matrix mtx = new Matrix();
mtx.postRotate(180f);

Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);