Android 无法从图库加载正确定向的图像

wol*_*ePk 5 java android opencv image orientation

我正在通过位图 api 从图库加载图像,但图像的方向与我保存的方向不同。在stackoverflow的某个地方,我读到我需要获取原始值,然后旋转矩阵。我试过这个,但我的方向来自 exif 界面的 0。

        int desiredImageWidth = 310;  // pixels
        int desiredImageHeight = 518; // pixels

        Log.i("FA", "Image Loading "+strFileName);
        BitmapFactory.Options o = new BitmapFactory.Options();
        Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(strFileName, o), 
                                                   desiredImageWidth, 
                                                   desiredImageHeight, 
                                                   false);

        Bitmap finalBmp = getCorrectOrientedBitmap(strFileName,newImage);

        Bitmap myBitmap32 = finalBmp.copy(Bitmap.Config.ARGB_8888, true);

        Mat matImg = Utils.bitmapToMat(myBitmap32);
Run Code Online (Sandbox Code Playgroud)

我的主要定位功能是

    private Bitmap getCorrectOrientedBitmap(String filename,Bitmap Source) throws IOException{

    Bitmap rotatedBitmap = null;
    ExifInterface exif = new ExifInterface(filename);
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

    Matrix matrix = new Matrix();
    Log.i("FA", "orientation "+orientation);
    switch(orientation)
    {
    case 6:
        matrix.postRotate(90);
        break;
    case 3:
        matrix.postRotate(180);
        break;
    case 8:
        matrix.postRotate(270);
        break;
    }
    rotatedBitmap = Bitmap.createBitmap(Source, 0, 0, Source.getWidth(), Source.getHeight(), matrix, true);

    return rotatedBitmap;
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 0

如果您从0回来getAttributeInt,那么我认为这仅意味着您的“旋转”图像中没有存储任何方向信息。您可以使用在线工具独立仔细检查或下载一个好的图像查看器来找出答案。

您是否在其他设备上尝试过此代码?您的测试设备是否可能不保存方向标签?

此页面有一些很好的示例代码来完成您想要做的事情,也许您也可以在那里找到一些改进。