其他手机也有可能出现旋转,可以统一处理
处理代码如下:
/**
* @desc : 测试小米 Max3图片有旋转
* @author : congge on 2020-06-23 13:56
**/
fun loadBitmap(imgpath: String?): Bitmap? {
if (imgpath.isNullOrBlank()){
return null
}
//原本的压缩逻辑
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imgpath, options)
options.inSampleSize = PhotoHelper.calculateInSampleSize(options, 480, 800)
options.inJustDecodeBounds = false
var bm = BitmapFactory.decodeFile(imgpath,options)
var digree = 0
var exif: ExifInterface? = null
exif = try {
ExifInterface(imgpath)
} catch (e: IOException) {
e.printStackTrace()
null
}
if (exif != null) {
// 读取图片中相机方向信息
val ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED)
digree = when (ori) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 0
}
LogUtil.i("exif",digree.toString())
}
if (digree != 0) {
// 旋转图片
val m = Matrix()
m.postRotate(digree.toFloat())
bm = Bitmap.createBitmap(bm, 0, 0, bm.width,
bm.height, m, true)
}
return bm
}
网友评论