最近项目中遇到用三星手机拍照,图片会自动旋转,应该是三星内部系统的功能,然后需要是不让他旋转,找到了方法。
原理就是,获取到图片,判断它的旋转角度,然后相应的旋转回来。
在拍照的返回结果中,获取到图片的路径。即activityforresult方法中。
path = filePath + fileName; //path 为拍照返回的路径
File file = new File(path);
int degree = readPictureDegree(file.getAbsolutePath());
Bitmap smallBitmap=FileUtils.compressSize(path, 800, 800);
smallBitmap = rotaingImageView(degree, smallBitmap);
saveAllPath = FileUtils.saveBitmap(smallBitmap, System.currentTimeMillis() +(Math.random() * 10000) + ".png");//质量压缩并存储得到path
ImageItem takePhoto = new ImageItem();
takePhoto.setBitmap(smallBitmap);
takePhoto.setImagePath(saveAllPath);
Bimp.tempSelectBitmap.add(takePhoto);
/**
* 尺寸压缩 public static Bitmap compressSize(final String path,final int w,final int h){
//Looper.prepare();
// TODO Auto-generated method stub
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置为ture只获取图片大小
opts.inJustDecodeBounds = true;//只读边,不读内容
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 返回为空
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
// 判断后缀名
String suffix = "";
CompressFormat format = null;
if (path.endsWith(".jpg")) {
suffix = ".jpg";
format = CompressFormat.JPEG;
} else if (path.endsWith(".jpeg")) {
suffix = ".jpeg";
format = CompressFormat.JPEG;
} else if (path.endsWith(".png")) {
suffix = ".png";
format = CompressFormat.PNG;
} else {
suffix = ".jpg";
format = CompressFormat.JPEG;
}
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {//如果宽度大于 传入的宽度 或者 高度大于 传入的高度大于
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
}
opts.inJustDecodeBounds = false;
//缩放后的高度和宽度取最大值
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (int) scale;//此处是最后的宽高值
Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts);
if (bMapRotate!=null) {
return bMapRotate;
}
return null;
}
/**
* 读取图片属性:旋转的角度}
/**
* 存储图片,返回路径
* @param bm
* @param picName
* @return
*/
public static String saveBitmap(Bitmap bm, String picName) {
try {
if (!isFileExist(picName)) {
File tempf = createSDDir("");
}
File f = new File(SDPATH, picName);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return SDPATH + picName;
}