经过测试,国产手机拍照无问题,国外手机拍照后自动选装90度了看图:主要有Google手机和三星手机都会有这个问题
解决办法也很简单说下思路:
首先获取图片被旋转的角度然后通过matrix.postRotate设置角度即可代码如下:
package com.wyze.mercury.common.utils;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
import java.io.IOException;
/**
* @author : xiayiye5
* @date : 2021/4/28 17:39
*/
public class CommonTools {
private static final CommonTools LOCATION_TOOLS = new CommonTools();
private CommonTools() {
}
public static CommonTools getInstance() {
return LOCATION_TOOLS;
}
/**
* 读取照片exif信息中的旋转角度
*
* @param path 照片路径
* @return 角度
*/
public int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
break;
}
} catch (IOException e) {
Log.e("exception msg", Log.getStackTraceString(e));
}
return degree;
}
public Bitmap toReturn(Bitmap img, String path) {
Matrix matrix = new Matrix();
/*翻转90度*/
matrix.postRotate(+readPictureDegree(path));
int width = img.getWidth();
int height = img.getHeight();
img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return img;
}
}
调用方法:
private fun setUploadImg(rlPicture: ImageView) {
imgPath.let {
val screenWidth = WpkCommonUtil.getScreenWidth();
val decodeFile = BitmapFactory.decodeFile(imgPath)
val width = decodeFile.width
val size = width / screenWidth
val newOpts = BitmapFactory.Options()
newOpts.inJustDecodeBounds = false
//设置缩放比例
newOpts.inSampleSize = size * 2
WpkLogUtil.e("打印缩放比例", "$size =")
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
val bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
rlPicture.setImageBitmap(CommonTools.getInstance().toReturn(bitmap, it))
}
看下前后效果图对比:
设置角度之前
设置角度之后
非常感谢原博主:原博主链接