从图库旋转图像位图

时间:2018-07-20 18:08:28

标签: java android image bitmap exif

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标头,其中包含旋转。

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

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

(对不起,英语不好,:-))

编辑:

错误的jpeg文件的EXIF代码是:

20180712_101743.jpg:JPEG图像数据,Exif标准:[TIFF图像数据,小端,direntries = 12,高度= 3096,制造商=三星,型号= SM-A310F,方向=右下,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,高度= 0,y分辨率= 187,方向= [ 0 ],软件= A310FXXU3CQL2,xresolution = 209,制造商=三星,宽度= 0],基线,精度8、4096x2606,帧3

2 个答案:

答案 0 :(得分:1)

拥有位图后,您还可以像这样创建其旋转副本

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

答案 1 :(得分:0)

在检测到图像是否需要旋转之后,请将此状态存储在布尔变量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);
    }