美文网首页
小米Max3上传图片旋转问题

小米Max3上传图片旋转问题

作者: 你的益达233 | 来源:发表于2020-08-08 15:47 被阅读0次

其他手机也有可能出现旋转,可以统一处理

处理代码如下:

/**
* @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
}

相关文章

网友评论

      本文标题:小米Max3上传图片旋转问题

      本文链接:https://www.haomeiwen.com/subject/buehdktx.html