在安卓中,EXIF方向表示照片的方向,可能是1、3、6或8,分别代表不旋转、旋转180度、顺时针旋转90度和逆时针旋转90度。
如果你想始终获取照片的方向为1,可以使用以下代码示例:
import android.media.ExifInterface;
// 获取照片的EXIF信息并修复方向
public static void fixOrientation(String photoPath) {
try {
ExifInterface exifInterface = new ExifInterface(photoPath);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
// 如果方向已经是1(不旋转),则无需修复
if (orientation == ExifInterface.ORIENTATION_NORMAL) {
return;
}
// 根据方向值进行旋转
int rotateAngle = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateAngle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateAngle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateAngle = 270;
break;
default:
return;
}
// 旋转照片
Matrix matrix = new Matrix();
matrix.postRotate(rotateAngle);
// 读取照片并进行旋转处理
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
// 保存旋转后的照片
FileOutputStream outputStream = new FileOutputStream(photoPath);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
// 回收内存
bitmap.recycle();
rotatedBitmap.recycle();
} catch (IOException e) {
e.printStackTrace();
}
}
你可以将要修复方向的照片路径传递给fixOrientation
方法,它会读取照片的EXIF信息并进行相应的旋转操作,最后保存旋转后的照片。
请注意,上述代码仅适用于旋转JPEG格式的照片。如果照片格式不同,可能需要进行相应的修改。