java - 在android中使用 picasso 从图像中读取EXIF数据

标签 java android picasso android-image exif

我有以下代码可以从 URL 下载图像并将其显示在 imageView 中:

Picasso.with(getActivity())
          .load(mImagesUrls[mPosition])
          .resize(500, 500)
          .centerCrop()
          .into(mSlideImage);

但除了显示图像之外,我还需要检索一些 EXIF 数据。谷歌搜索如何为 android 执行此操作将我带到“ExifInterface”类,但它的构造函数如下:

  1. ExifInterface(String filename)
    • Reads Exif tags from the specified image file.
  2. ExifInterface(FileDescriptor fileDescriptor)
    • Reads Exif tags from the specified image file descriptor.
  3. ExifInterface(InputStream inputStream)
    • Reads Exif tags from the specified image input stream.

我如何获得这些构造函数所需的任何参数?我在 Picasso 中所能找到的只是一种将图像作为位图加载的方法,它似乎不支持 EXIF 数据。

最佳答案

我不知道它是否有帮助,但我已经举了一个在我的案例中有效的例子,但这只是一个例子:

public void transformPicture(final File file, final OnImageReady onImageReady) {
float rotation = 0;
try {
    Uri uri = Uri.fromFile(file);
    ExifInterface exifInterface = new ExifInterface(uri.getPath());
    int orientation = exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL);
    switch (orientation){
    case ExifInterface.ORIENTATION_ROTATE_90: {
        rotation = -90f;
        break;
    }
    case ExifInterface.ORIENTATION_ROTATE_180: {
        rotation = -180f;
        break;
    }
    case ExifInterface.ORIENTATION_ROTATE_270: {
        rotation = 90f;
        break;
    }
    }
} catch (IOException e) {
    e.printStackTrace();
}

Picasso.get().load(file)
    .resize(getTargetMaxSize(), getTargetMaxSize())
    .centerInside()
    .rotate(rotation)
    .into(myView);
}

请记住,您只能从文件中读取 exif,而不能从网络中读取。 也可以看到Exif Orientation Tag

你甚至可以像这样获得 ExifInterface:

String fileName = "/path/file.any";
ExifInterface exifInterface1 = new ExifInterface(fileName);

FileInputStream fis = new FileInputStream(new File(fileName));
ExifInterface exifInterface3 = new ExifInterface(fis);

关于java - 在android中使用 picasso 从图像中读取EXIF数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39007883/

相关文章:

android - Flutter VSCode:调试错误:无法使用BuildSessionServices.createFileHasher()创建FileHasher类型的服务

java - 为什么 Android 上的 ExifInterface 会出现“文件未找到”错误?

android - 通过传递身份验证 token Picasso Xamarin 加载图像

java - 哈希码冲突会影响 Java 中的 equals 吗?

java - 我如何调用 sqljdbc_auth.dll

java - 如何在单元测试中模拟 REST API?

java - 在jsplitpane中的jscrollpane中调整 Canvas 大小问题

android - 如何将文件从 'assets' 文件夹复制到 SD 卡?

java - 模拟 GPS 位置问题

android - 在 Picasso 客户端上设置自定义 HTTP 客户端缓存的影响