【问题标题】:Set image orientation using ExifInterface使用 ExifInterface 设置图像方向
【发布时间】:2017-03-03 00:30:32
【问题描述】:

Camera.Parameters 中的setRotation method 不适用于所有设备。有人建议手动更改EXIF信息来解决问题。您能否给我一个简短的示例,说明如何使用ExifInterface 设置exif 信息,从而将图像方向设置为纵向?

private int savePicture(byte[] data)
{
       File pictureFile = getOutputMediaFile();
       if (pictureFile == null)
           return FILE_CREATION_ERROR;

       try {
           FileOutputStream fos = new FileOutputStream(pictureFile);
           fos.write(data);
           fos.close();
       } catch (FileNotFoundException e) {
           return FILE_NOT_FOUND;
       } catch (IOException e) {
           return ACCESSING_FILE_ERROR;
       }

   return OKAY;
}

我试过这个:

    try {
        ExifInterface exifi = new ExifInterface(pictureFile.getAbsolutePath());
        exifi.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
        exifi.saveAttributes();
    } catch (IOException e) {
        Log.e(TAG, "Exif error");
    }

但当我从 android 图库中可视化图片时,没有任何变化。

【问题讨论】:

    标签: android orientation exif


    【解决方案1】:

    对于那些真正想写出这些EXIF信息的人,这里有一些代码:

    ExifInterface exifInterface = new ExifInterface(someFile.getPath());
    exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
                               String.valueOf(orientation));
    exifInterface.saveAttributes();
    

    orientation 是标准方向之一,即ExifInterface.ORIENTATION_ROTATE_{90,180,270}

    【讨论】:

    • 这与 OP 所说的他/她尝试过的有何不同?
    • Hrm...除了我没有滚动到顶部帖子的右侧之外,我找不到任何借口... :(
    • 不用担心。很高兴知道我没有遗漏什么
    【解决方案2】:

    如果方向已保存到文件中但未出现在图库中,则可能是因为方向已缓存在 MediaStore 中。因此,您还需要尝试在此处更新此信息。

    这里是截取的代码(未经测试)

    /**
     * @param fileUri the media store file uri
     * @param orientation in degrees 0, 90, 180, 270
     * @param context
     * @return
     */
    public boolean setOrientation(Uri fileUri, int orientation, Context context) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.ORIENTATION, orientation);
        int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
        return rowsUpdated > 0;
    }
    
    /**
     * Get content uri for the file path
     * 
     * @param path
     * @param context
     * @return
     */
    public Uri getContentUriForFilePath(String path, Context context) {
        String[] projection = {
            MediaStore.Images.Media._ID
        };
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                MediaStore.Images.Media.DATA + " = ?", new String[] {
                    path
                }, null);
        Uri result = null;
        if (cursor != null) {
            try {
                if (cursor.moveToNext()) {
                    long mediaId = cursor.getLong(0);
                    result = ContentUris.withAppendedId(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaId);
                }
            } finally {
                cursor.close();
            }
        }
        return result;
    }
    

    【讨论】:

      【解决方案3】:

      好吧,我遇到了这样的问题,并尝试了您正在研究的解决方案并最终使用了 Matrix 函数。

      无论我拍什么图片,都是风景,所以在应用程序中我只是应用了以下代码,它运行良好:-

      Matrix matrix = new Matrix();
      //set image rotation value to 90 degrees in matrix.
      matrix.postRotate(90);
      //supply the original width and height, if you don't want to change the height and width of //bitmap.
      Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
      

      【讨论】:

      • 好的,但是如果我们这样做会导致很多性能下降:1)将 byte[] 数据写入文件 2)读取文件并提取 exif 信息 3)解压缩文件到 bmp 4) 旋转图像 5) 在 jpeg 中再次压缩 6) 覆盖文件。我想知道是否有更简单的解决方案可以直接作用于 data[]
      • 你处理问题的方式,会降低应用程序的性能,明智地使用它,不需要像你解释的那样处理图像,保存图像没有需要应用您解释的所有输入,当您需要在视图中显示时,在运行时执行并明智地处理查看图像,它不会降低其性能。
      • 我不同意你的看法,Bette。我想使用 android 库来显示图像,而 android 库以错误的方向显示它们。
      • 您不同意哪一点?为什么要让您的图像在画廊中可见?查看链接,如果它们对您有用。 android.googlesource.com/platform/frameworks/base/+/master/…stackoverflow.com/questions/5186795/…
      • 不,我的意思是我不想发展自己的画廊。为了浏览我使用正在开发的相机应用程序拍摄的照片,我想使用已安装在任何设备中的图库应用程序。但是,使用 android.development 中的示例代码和一些在线教程中的手机纵向拍摄的图片无法使用,因为它们以错误的方向显示。
      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      相关资源
      最近更新 更多