老规矩,先上效果图(虽然说不是很形象吧):
1.首先我们获取一下相机拍照后翻转的角度
/**
* 读取照片exif信息中的旋转角度
* @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;
}
2.上面方法返回的就是旋转的角度,然后我们将他按照角度在重新翻转回来,返回的是Bitmap对象
//旋将旋转后的图片翻转
public static Bitmap toturn(String path, int degree){
Bitmap img = BitmapFactory.decodeFile(path);
Matrix matrix = new Matrix();
matrix.postRotate(degree); /*翻转90度*/
int width = img.getWidth();
int height =img.getHeight();
img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return img;
}
3.我们要定义两个变量为本地存储的路径
private static final String SD_PATH = "/sdcard/dskqxt/pic/";
private static final String IN_PATH = "/dskqxt/pic/";
4.然后随机去获取文件名字
/**
* 随机生产文件名
* @return
*/
private static String generateFileName() {
return UUID.randomUUID().toString();
}
5.最后将Biamap传入这个方法,返回的就是本地存储的路径
//将bitmap对象写到本地路径
public static String saveBitmap(Context context, Bitmap mBitmap) {
String savePath;
File filePic;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
savePath = SD_PATH;
} else {
savePath = context.getApplicationContext().getFilesDir()
.getAbsolutePath()
+ IN_PATH;
}
try {
filePic = new File(savePath + generateFileName() + ".jpg");
if (!filePic.exists()) {
filePic.getParentFile().mkdirs();
filePic.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePic);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return filePic.getAbsolutePath();
}
6.现在我们直接就可以拿5返回的String类型的图片路径去操作就可以了