解决问题
直接进入正题
当我们上传图片需要做的处理
- 压缩
- 判断角度是否正确
- 如果不角度不对,纠正角度
- 其他(加水印什么的看具体需求)
解决
以下代码块
/**
* 压缩图片,处理某些手机拍照角度旋转的问题
* @param context
* @param filePath
* @param file
* @param q
* @return
* @throws FileNotFoundException
*/
public static File compressImage(Context context, String filePath, File file, int q) throws FileNotFoundException {
Bitmap bm = getSmallBitmap(filePath);
int degree = readPictureDegree(filePath);
LogUtil.e("fengan", "degree==" + degree);
if (degree != 0) {//旋转照片角度
bm = rotateBitmap(bm, degree);
}
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, q, out);
return file;
}`
/**
* 获取图片角度
* @param path
* @return
*/
public static 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;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 旋转照片
* @param bitmap
* @param degress
* @return
*/
public static Bitmap rotateBitmap(Bitmap bitmap, int degress) {
if (bitmap != null) {
Matrix m = new Matrix();
m.postRotate(degress);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), m, true);
return bitmap;
}
return bitmap;
}
/**
* 根据路径获得突破并压缩返回bitmap用于显示
*
* @return
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}