繁体   English   中英

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

[英]Android unable to load correct oriented image from gallery

我正在通过位图api从图库加载图像,但图像的方向与我保存的图像的方向不同。 在stackoverflow的某处,我读到我需要获取oreintation值然后旋转矩阵。 我尝试了这个但是我的方向从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);

我的主要定位功能是

    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;
}

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

你在其他设备上试过这个代码吗? 您的测试设备是否可能无法保存方向标签?

这个页面有一些很好的示例代码可以执行您正在尝试的操作,也许您可​​以在那里找到一些改进。

暂无
暂无

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

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