reading android jpeg EXIF metadata from picture callback

前端 未结 8 1838
天命终不由人
天命终不由人 2020-11-30 20:48

Background: I am writing a camera app for a messenger program. I cannot save the captured image to persistent disk at any time. The camera must support all orientations.

8条回答
  •  情书的邮戳
    2020-11-30 21:34

    So using my edit and pcans suggestion, I got the image data but it was not what I expected. Specifically, not all devices will give an orientation at all. If you follow this path note that

    • The "Android fixed" ExifReader library I point to is actually the edited 2.3.1 which is a few releasees old. The new examples on the website and in the source pertain to the newest 2.6.x where he changes the API significantly. Using the 2.3.1 interface, you can dump all EXIF data from a byte[] by doing the following:

              Metadata header;    
              try {
                  ByteArrayInputStream bais= new ByteArrayInputStream(data);
                  ExifReader reader = new ExifReader(bais);
                  header = reader.extract();
                  Iterator iter = header.getDirectoryIterator();
                  while(iter.hasNext()){
                     Directory d = iter.next();
                     Iterator iterTag = d.getTagIterator();
                     while(iterTag.hasNext()){
                        Tag t = iterTag.next();
                        Log.e("DEBUG", "TAG: " + t.getTagName() + " : " + t.getDescription());
                     }
                  }
              } catch (JpegProcessingException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (MetadataException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

    if you want numerical tag values, simply replace

    t.getDescription()
    

    with

    d.getInt(t.getTagType())
    
    • Although ExifReader has a constructor using byte[], I must have misunderstood what it expects because if I try to use it with the data array directly, I get to Tags in the returned directory.

    I really didn't add much as far as the answer is concerned so I'm accepting pcans' answer.

提交回复
热议问题