平方X

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2364|回复: 0

[2500]裁剪图片被旋转

[复制链接]

414

主题

709

帖子

3602

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3602
QQ
发表于 2018-1-4 15:36:39 | 显示全部楼层 |阅读模式
[md]
使用的库为 [jdamcd/android-crop](https://github.com/jdamcd/android-crop)  
选择它,因为其
> based on code from AOSP

但是小米 6 测试出现了裁剪图片不正常的 bug,表现为图片长宽正常,但相比原图发生了旋转而不能正常显示。  
后来发现同一张照片就是会出现问题。  

首先想到是不是有 bug,于是查看 [issues](https://github.com/jdamcd/android-crop/issues)  
果然有不少人提出了相同的问题,想在 issues 中找找有没有解决方法。  
后来着重在 [pull requests](https://github.com/jdamcd/android-crop/pulls) 中查看。  
确实有不少的请求,也有一些给出了解方案,整理如下:

# 解决方案
添加旋转,去除 copyExifRotation ,去除预览。

以下是一些请求

[Uses ExifInterface from Support Library and saves output rotated without EXIF #251](https://github.com/jdamcd/android-crop/pull/251)

使用了 Support Library ,测试成功,但是不想引入库。  
同时提供了注释
>For the same reason (to be protocol independent), the EXIF information is not copied
to the output anymore. Instead, the cropped image is rotated using Matrix.postRotate
(as a result of this, it is not necessary to rewrite the output just to copy the EXIF
rotation as done before).


[Fix Rotation Errors, withJpgQuality() added, Photo Action, Clean code (Min SDK 19) #190](https://github.com/jdamcd/android-crop/pull/190)

知道了不需要展示,可以注掉,上面 251 的解决方案,会在裁剪后先展示旋转的图,再返回,不展示以后更好了。
```
        if (croppedImage != null) {
            //imageView.setImageRotateBitmapResetBase(new RotateBitmap(croppedImage, exifRotation), true);
            //imageView.center();
            imageView.highlightViews.clear();
        }
```
后面的处理也是正确的处理,除了包含 copyExifRotation
```
                //if the cutting box are rectangle( outWidth != outHeight ),and the exifRotation is 90 or 270,
                //the outWidth and outHeight should be interchanged
                 if (exifRotation==90 || exifRotation==270) {
                     int temp=outWidth;
                     outWidth=outHeight;
                     outHeight=temp;
                 }
                 ...
                croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
                Matrix matrix = new Matrix();
                if (rect.width() > outWidth || rect.height() > outHeight) {
                    matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
                }
                //If the picture's exifRotation !=0 ,they should be rotated to 0 degrees
                //If the picture need not to be scale, they also need to be rotate to 0 degrees
                matrix.postRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
```

[A bug about exifRotation #146](https://github.com/jdamcd/android-crop/pull/146)

有图,有注释,很棒,但是仍然有 copyExifRotation  
可以看到 190 的参考了这个 146 的。
```
                croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
                if (rect.width() > outWidth || rect.height() > outHeight) {
                    Matrix matrix = new Matrix();
                    matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());

                    //if the picture's exifRotation !=0 ,they should be rotate to 0 degrees
                    matrix.postRotate(exifRotation);
                    croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
                }else{
                    //if the picture need not to be scale, they also neet to be rotate to 0 degrees
                    Matrix matrix = new Matrix();
                    matrix.postRotate(exifRotation);
                    croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
                }
```

[Fixed wrong work with exif #116](https://github.com/jdamcd/android-crop/pull/116)

简单粗暴,注释了 copyExifRotation ,加上了 matrix.setRotate(exifRotation);   
但是应该加到外面才对,而且没有处理交换宽高。

[/md]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|平方X ( 冀ICP备14018164号 )

GMT+8, 2024-4-25 08:14 , Processed in 0.130464 second(s), 21 queries .

技术支持:Powered by Discuz!X3.4  © 2001-2013 Comsenz Inc.

版权所有:Copyright © 2014-2018 平方X www.pingfangx.com All rights reserved.

快速回复 返回顶部 返回列表