在Android中使用Picasso从图像中读取EXIF数据

时间:2016-08-17 23:44:51

标签: java android picasso android-image exif

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

function

但除了显示图像外,我还需要检索一些EXIF数据。谷歌搜索如何为Android做这个导致我进入" ExifInterface"但是它的构造函数如下:

  
      
  1. ExifInterface(String filename)      
        
    • 从指定图片文件中读取Exif标记。
    •   
  2.   
  3. ExifInterface(FileDescriptor fileDescriptor)      
        
    • 从指定图像文件描述符中读取Exif标记。
    •   
  4.   
  5. ExifInterface(InputStream inputStream)      
        
    • 从指定图片输入流中读取Exif标记。
    •   
  6.   

我怎样才能得到这些构造函数需要的任何参数?毕加索所能找到的只是一种将图像作为位图加载的方法,它似乎不支持EXIF数据。

1 个答案:

答案 0 :(得分:0)

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

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