コード例 #1
0
 public TIFFNode getExifMetadata() throws IOException {
   readHeader();
   return er.getMetaDataTree();
 }
コード例 #2
0
  /** Reads the header. Does nothing if the header has already been loaded. */
  private void readHeader() throws IOException {
    if (numImages == -1) {
      ImageInputStream in = (ImageInputStream) getInput();
      in.seek(0);
      er = new EXIFReader(in);
      er.setFirstImageOnly(false);
      er.read();

      // Get some information that is easy to obtain through a map
      {
        HashMap<TIFFTag, TIFFField> m = er.getMetaDataMap();
        TIFFField mde;
        if ((mde = m.get(MPFTagSet.get(MPFTagSet.TAG_NumberOfImages))) != null) {
          numImages = ((Number) mde.getData()).intValue();
        } else {
          numImages = 1;
        }
        if ((mde = m.get(EXIFTagSet.PixelXDimension)) != null) {
          width = ((Number) mde.getData()).intValue();
        }
        if ((mde = m.get(EXIFTagSet.PixelYDimension)) != null) {
          height = ((Number) mde.getData()).intValue();
        }
      }
      imageOffsets = new long[numImages];
      imageLengths = new long[numImages];
      if (numImages == 1) {
        imageOffsets[0] = 0;
        imageLengths[0] = in.length();
      }

      // Get now at the tough part
      int index = 0;
      for (Iterator<TIFFNode> e = er.getMetaDataTree().preorderIterator(); e.hasNext(); ) {
        TIFFNode n = e.next();
        if (n instanceof TIFFDirectory) {
          TIFFDirectory dir = (TIFFDirectory) n;
          // System.out.println("dir:" + dir.getName());
          if (dir.getName() != null && dir.getName().equals("MPEntry")) {
            long dirOffset = dir.getFileSegments().get(0).getOffset();
            TIFFField offsetField = dir.getField(MPEntryTagSet.IndividualImageDataOffset);
            TIFFField lengthField = dir.getField(MPEntryTagSet.IndividualImageSize);
            if (offsetField != null && lengthField != null) {
              long dataOffset = (Long) offsetField.getData();
              imageOffsets[index] = dataOffset == 0 ? 0 : dirOffset + dataOffset;
              imageLengths[index] = (Long) lengthField.getData();
              index++;
            }
          }
        }
      }

      // Store metadata for later access
      String formatName = "com_sun_media_imageio_plugins_tiff_image_1.0";
      imageMetadata = new IIOMetadata[numImages];
      for (int i = 0; i < numImages; i++) {
        imageMetadata[i] = new DefaultIIOMetadata(formatName, er.getIIOMetadataTree(formatName, i));
      }

      in.seek(0);
    }
  }