Android部分⼿机拍照后获取的图⽚被旋转问题的解决⽅
调⽤Android系统拍照功能后,三星⼿机拍摄后的照⽚被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的。其它品牌的⼿机都是正常的,就三星出现这个怪事。
在Android适配上,我原来⼀直以为国内的⼩⽶⼿机够奇葩了,结果还有更奇葩的!你说你没事旋转照⽚⼲啥,实在是猜不透其居⼼何在,纯粹是在给开发者制造⿇烦啊!
解决办法是获取到拍照后照⽚被旋转的⾓度,再旋转回去就好了。
具体思路:
1、⾸先在调⽤拍照⽅法时,保存拍照后的相⽚原图,得到原图路径,(PhotoBitmapUtils是我⾃⼰写的⼀个⼯具类)
String fileName = "";
/**
* 启动相机拍照
*/
private void addBitmapShoots() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置图⽚要保存的根路径+⽂件名
mkdirs方法
fileName = PhotoFileName(getContext());
File file = new File(fileName);
if (!ists()) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, OPEN_CAMERA);
}
2、在获取相机返回的回调⽅法onActivityResult()中,修复被旋转的图⽚并取得修复后的图⽚路径,有了这个路径后就可以展⽰出来了
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// 获取相机拍照返回
if (resultCode == Activity.RESULT_OK && requestCode == OPEN_CAMERA) {
// 得到修复后的照⽚路径
String filepath = PhotoBitmapUtils.amendRotatePhoto(fileName, getContext());
}
}
PhotoBitmapUtils类:
/**
* 集合⼀些图⽚⼯具
*
* Created by zhuwentao on 2016-07-22.
*/
public class PhotoBitmapUtils {
/**
* 存放拍摄图⽚的⽂件夹
*/
private static final String FILES_NAME = "/MyPhoto";
/**
* 获取的时间格式
*/
public static final String TIME_STYLE = "yyyyMMddHHmmss";
/**
* 图⽚种类
*/
public static final String IMAGE_TYPE = ".png";
// 防⽌实例化
private PhotoBitmapUtils() {
}
/**
* 获取⼿机可存储路径
*
* @param context 上下⽂
* @return ⼿机可存储路径
*/
private static String getPhoneRootPath(Context context) {
// 是否有SD卡
if (ExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| !Environment.isExternalStorageRemovable()) {
// 获取SD卡根⽬录
ExternalCacheDir().getPath();
} else {
// 获取apk包下的缓存路径
CacheDir().getPath();
}
}
/
**
* 使⽤当前系统时间作为上传图⽚的名称
*
* @return 存储的根路径+图⽚名称
*/
public static String getPhotoFileName(Context context) {
File file = new File(getPhoneRootPath(context) + FILES_NAME);
// 判断⽂件是否已经存在,不存在则创建
if (!ists()) {
file.mkdirs();
}
/
/ 设置图⽚⽂件名称
SimpleDateFormat format = new SimpleDateFormat(TIME_STYLE, Default());  Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
String photoName = "/" + time + IMAGE_TYPE;
return file + photoName;
}
/**
* 保存Bitmap图⽚在SD卡中
* 如果没有SD卡则存在⼿机中
*
* @param mbitmap 需要保存的Bitmap图⽚
* @return 保存成功时返回图⽚的路径,失败时返回null
*/
public static String savePhotoToSD(Bitmap mbitmap, Context context) {
FileOutputStream outStream = null;
String fileName = getPhotoFileName(context);
try {
outStream = new FileOutputStream(fileName);
// 把数据写⼊⽂件,100表⽰不压缩
mbitmappress(Bitmap.CompressFormat.PNG, 100, outStream);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (outStream != null) {
// 记得要关闭流!
outStream.close();
}
if (mbitmap != null) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 把原图按1/10的⽐例压缩
*
* @param path 原图的路径
* @return 压缩后的图⽚
*/
public static Bitmap getCompressPhoto(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; // 图⽚的⼤⼩设置为原来的⼗分之⼀
Bitmap bmp = BitmapFactory.decodeFile(path, options);
options = null;
return bmp;
}
/**
* 处理旋转后的图⽚
* @param originpath 原图路径
* @param context 上下⽂
* @return 返回修复完毕后的图⽚路径
*/
public static String amendRotatePhoto(String originpath, Context context) {
// 取得图⽚旋转⾓度
int angle = readPictureDegree(originpath);
// 把原图压缩后得到Bitmap对象
Bitmap bmp = getCompressPhoto(originpath);;
// 修复图⽚被旋转的⾓度
Bitmap bitmap = rotaingImageView(angle, bmp);
// 保存修复后的图⽚并返回保存后的图⽚路径
return savePhotoToSD(bitmap, context);
}
/**
* 读取照⽚旋转⾓度
*
* @param path 照⽚路径
* @return ⾓度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = AttributeInt(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;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 旋转图⽚
* @param angle 被旋转⾓度
* @param bitmap 图⽚对象
* @return 旋转后的图⽚
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// 根据旋转⾓度,⽣成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 将原始图⽚按照旋转矩阵进⾏旋转,并得到新的图⽚
returnBm = ateBitmap(bitmap, 0, 0, Width(), Height(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bitmap;
}
if (bitmap != returnBm) {
}
return returnBm;
}
}
在调⽤修复图⽚⾓度⽅法的时候需要注意,现在的⼿机像素越来越⼤,拍完后⼀张照⽚有近10M,所以我们需要对图⽚进⾏压缩处理。不然在保存图⽚时会等待挺久的,屏幕会⿊⼀会。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。