我有以下代码可以从 URL 下载图像并将其显示在 imageView 中:
Picasso.with(getActivity())
.load(mImagesUrls[mPosition])
.resize(500, 500)
.centerCrop()
.into(mSlideImage);
但除了显示图像之外,我还需要检索一些 EXIF 数据。谷歌搜索如何为 android 执行此操作将我带到“ExifInterface”类,但它的构造函数如下:
- ExifInterface(String filename)
- Reads Exif tags from the specified image file.
- ExifInterface(FileDescriptor fileDescriptor)
- Reads Exif tags from the specified image file descriptor.
- 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/