首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >旋转位图而不使用ImageURI/ContentResolver?

旋转位图而不使用ImageURI/ContentResolver?
EN

Stack Overflow用户
提问于 2017-03-03 22:00:02
回答 1查看 262关注 0票数 0

我试图适应微软的projectOxford EmotionApi的图像-自动旋转代码。对设备摄像机拍摄的每一幅图像进行角度分析,然后旋转到正确的景观视图上,通过情感API进行分析。

我的问题是:我如何调整下面的代码,使其以位图为论据?在这种情况下,我也完全不知道Content和ExitInterface的角色。任何帮助都是非常感谢的。

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-03 23:33:27

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

你不能这么做。

在这种情况下,我也完全不知道内容解析器和ExitInterface的作用。

您问题中的代码使用EXIF Orientation标记来确定应用于图像的方向,就像拍摄照片的摄像机(或其他设置的标记)所报告的那样。ExifInterface是读取EXIF标记的代码。ExifInterface需要处理实际的Bitmap数据,而不是解码的Bitmap-- Bitmap不再有EXIF标记。

里面的ContentResolver代码是错误的,不应该使用.ExifInterface库中的com.android.support:exifinterface有一个构造函数,该构造函数接受InputStream,它将从该构造函数读取InputStream。这里使用Uri的正确方法是将它传递给ContentResolver上的openInputStream(),将该流传递给ExifInterface构造函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42589171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档