ram*_*ral 103

如果图像(照片)是由您制作的程序拍摄的,则必须使用正确的旋转值设置Parameters.setRotation.

这取决于相机驱动,在保存之前旋转图像或将旋转值保存到exif TAG_ORIENTATION.

因此,如果TAG_ORIENTATION为null或零,则图像的方向正确,否则您必须根据TAG_ORIENTATION中的值旋转图像.

从EXIF获取方向:

ExifInterface exif = null;
try {
    exif = new ExifInterface(path);
} catch (IOException e) {
    e.printStackTrace();
}  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED);
Run Code Online (Sandbox Code Playgroud)

旋转位图:

Bitmap bmRotated = rotateBitmap(bitmap, orientation);  
Run Code Online (Sandbox Code Playgroud)

旋转位图的方法:

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
       case ExifInterface.ORIENTATION_ROTATE_90:
           matrix.setRotate(90);
           break;
       case ExifInterface.ORIENTATION_TRANSVERSE:
           matrix.setRotate(-90);
           matrix.postScale(-1, 1);
           break;
       case ExifInterface.ORIENTATION_ROTATE_270:
           matrix.setRotate(-90);
           break;
       default:
           return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return bmRotated;
    }
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用exif总是只返回0 (27认同)
  • 为了避免OutOfMemoryError,我只是在将Bitmap传递给rotateBitmap()函数之前重新调整了它的位置:Bitmap myBitmap = BitmapFactory.decodeFile(picturePath); 位图已调整大小= Bitmap.createScaledBitmap(myBitmap,720,1280,true); photo = rotateBitmap(picturePath,resized); (3认同)

Ank*_*pli 5

对我来说,ExifInterface 工作得很好,就像这样:

ExifInterface exifInterface = new ExifInterface(imagePath);
degree = Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION));
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试使用以下方法获取图像的详细信息MediaStore

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
} 
Run Code Online (Sandbox Code Playgroud)

类似的解决方案:ExifInterface 总是返回 1

希望能帮助到你.. :)