java - Android 无法从图库中加载正确方向的图像

标签 java android image opencv 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);

我的主要定位功能是

    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,那么我认为这只是意味着您的“旋转”图像中没有存储任何方向信息。您可以独立地使用在线工具仔细检查或下载一个好的图像查看器来找出答案。

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

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

关于java - Android 无法从图库中加载正确方向的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10717136/

相关文章:

java - 在单元测试中使用接口(interface)

java - JNI NewStringUTF 调用时出现未决异常 'java.lang.NoSuchMethodError'

android - 自定义按钮不适用于 OnTouchListener

android - 更新 Android SDK 工具

android - 在 SQLite 中保存资源路径

html - 你如何使图像闪烁?

java - NoSuchMethodError : org. openqa.selenium.os.CommandLine.waitFor(J)V 与 webdriver v2.45.0 和 FF v33

python - 使用滚动,俯仰和偏航使图像变形

Javascript 将动态图像从 URL 绘制到 Canvas 元素上

java - 如何使用 hibernate 查询从电子邮件列表中过滤邮件域