简体   繁体   English

Android 使用 Camera2Api 拍照时图像旋转

[英]Android Image is rotated when I take a picture using Camera2Api

Does anyone know various rotation angles for mainstream phones.有谁知道主流手机的各种旋转角度。 Everytime I take a picture it always rotates it at different angles on different mobile devices.每次我拍照时,它总是在不同的移动设备上以不同的角度旋转它。

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees.大多数手机相机都是横向的,这意味着如果您以纵向拍摄照片,生成的照片将旋转 90 度。 In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in. Though this is not 100% reliable because it depends on the camera software populating the Exit data在这种情况下,相机软件应使用查看照片的方向填充 Exif 数据。虽然这不是 100% 可靠,因为它取决于填充退出数据的相机软件

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                     ExifInterface.ORIENTATION_UNDEFINED);

Bitmap rotatedBitmap = null;
switch(orientation) {

    case ExifInterface.ORIENTATION_ROTATE_90:
        rotatedBitmap = rotateImage(bitmap, 90);
        break;

    case ExifInterface.ORIENTATION_ROTATE_180:
        rotatedBitmap = rotateImage(bitmap, 180);
        break;

    case ExifInterface.ORIENTATION_ROTATE_270:
        rotatedBitmap = rotateImage(bitmap, 270);
        break;

    case ExifInterface.ORIENTATION_NORMAL:
    default:
        rotatedBitmap = bitmap;
}

Method to rotateimage旋转图像的方法

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                               matrix, true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM