在不使用ImageUri/ContentResolver的情况下旋转位图



我正在尝试调整Microsoft的Projectoxford Emotionapi的图像 - 自动Rotater代码。分析设备相机拍摄的每个图像的角度,然后旋转到正确的景观视图,以通过情感API分析。

我的问题是:我如何调整下面的代码以将位图作为参数?在这种情况下,我也完全失去了内容解析器和Exitinterface的作用。任何帮助都非常感谢。

private static int getImageRotationAngle(
        Uri imageUri, ContentResolver contentResolver) throws IOException {
    int angle = 0;
    Cursor cursor = contentResolver.query(imageUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            angle = cursor.getInt(0);
        }
        cursor.close();
    } else {
        ExifInterface exif = new ExifInterface(imageUri.getPath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                angle = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                angle = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                angle = 90;
                break;
            default:
                break;
        }
    }
    return angle;
}
// Rotate the original bitmap according to the given orientation angle
private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
    // If the rotate angle is 0, then return the original image, else return the rotated image
    if (angle != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(
                bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } else {
        return bitmap;
    }
}

我将如何调整下面的代码以将位图作为参数?

你不能。

在这种情况下,我也完全失去了内容解析器和Exitinterface的作用

您问题中的代码使用EXIF Orientation标签来确定应应用于图像的方向,如拍摄照片的相机报道(或该标签的任何设置)所报告。ExifInterface是读取EXIF标签的代码。ExifInterface需要使用实际的JPEG数据,而不是解码的Bitmap—Bitmap不再具有EXIF标签。

ContentResolver代码中有错误窗口,不应使用。com.android.support:exifinterface库中的ExifInterface具有一个构造函数,该构造函数采用InputStream,从中读取JPEG。在此处使用Uri的正确方法是将其传递给ContentResolver上的openInputStream(),将该流传递到ExifInterface构造函数。

最新更新