title: Extracting Metadata in Java
body: <p>How do I extract metadata from ONLY JPEG images using Java? I tried the codes on <a href="https://stackoverflow.com/questions/16115851/read-image-metadata-from-single-file-with-java">Read Image Metadata from single file with Java</a> </p>
  
  <pre><code>import org.w3c.dom.*;
  
  import java.io.*;
  import java.util.*;
  import javax.imageio.*;
  import javax.imageio.stream.*;
  import javax.imageio.metadata.*;
  
  public class Metadata {
  
  public static void main(String[] args) {
      Metadata meta = new Metadata();
      String filename = "C:\\Users\\Public\\Pictures\\download.jpg";
      if (new File(filename).exists()) {
          meta.readAndDisplayMetadata(filename);
      } else {
          System.out.println("cannot find file: " + filename);
      }
  
  }
  
  void readAndDisplayMetadata( String fileName ) {
      try {
  
          File file = new File( fileName );
          ImageInputStream iis = ImageIO.createImageInputStream(file);
          Iterator&lt;ImageReader&gt; readers = ImageIO.getImageReaders(iis);
  
          if (readers.hasNext()) {
  
              // pick the first available ImageReader
              ImageReader reader = readers.next();
  
              // attach source to the reader
              reader.setInput(iis, true);
  
              // read metadata of first image
              IIOMetadata metadata = reader.getImageMetadata(0);
  
              String[] names = metadata.getMetadataFormatNames();
              int length = names.length;
              for (int i = 0; i &lt; length; i++) {
                  System.out.println( "Format name: " + names[ i ] );
                  displayMetadata(metadata.getAsTree(names[i]));
              }
          }
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  void displayMetadata(Node root) {
  displayMetadata(root, 0);
  }
  
  void indent(int level) {
  for (int i = 0; i &lt; level; i++)
      System.out.print("    ");
  }
  
  void displayMetadata(Node node, int level) {
  // print open tag of element
  indent(level);
  System.out.print("&lt;" + node.getNodeName());
  NamedNodeMap map = node.getAttributes();
  if (map != null) {
  
      // print attribute values
      int length = map.getLength();
      for (int i = 0; i &lt; length; i++) {
          Node attr = map.item(i);
          System.out.print(" " + attr.getNodeName() +
                           "=\"" + attr.getNodeValue() + "\"");
      }
  }
  
  Node child = node.getFirstChild();
  if (child == null) {
      // no children, so close element and return
      System.out.println("/&gt;");
      return;
  }
  
  // children, so close current tag
  System.out.println("&gt;");
  while (child != null) {
      // print children recursively
      displayMetadata(child, level + 1);
      child = child.getNextSibling();
  }
  
  // print close tag of element
  indent(level);
  System.out.println("&lt;/" + node.getNodeName() + "&gt;");
  }
  }
  </code></pre>
  
  <p>this is the code that i used. the output that i got is a little weird. the metadata that i want to extract is the filename, the file type, MIME type, dimensions, encoding process, bits per sample, color channels, file size, JFIF version, resolution unit, x and y resolution as well as MD5 and SHA1 hash values.</p>
  
  <p>OUTPUT:</p>
  
  <pre><code>Format name: javax_imageio_jpeg_image_1.0
  &lt;javax_imageio_jpeg_image_1.0&gt;
  &lt;JPEGvariety&gt;
      &lt;app0JFIF majorVersion="1" minorVersion="1" resUnits="1" Xdensity="96"    Ydensity="96" thumbWidth="0" thumbHeight="0"/&gt;
  &lt;/JPEGvariety&gt;
  &lt;markerSequence&gt;
      &lt;unknown MarkerTag="225"/&gt;
      &lt;unknown MarkerTag="225"/&gt;
      &lt;dqt&gt;
          &lt;dqtable elementPrecision="0" qtableId="0"/&gt;
      &lt;/dqt&gt;
      &lt;dqt&gt;
          &lt;dqtable elementPrecision="0" qtableId="1"/&gt;
      &lt;/dqt&gt;
      &lt;sof process="0" samplePrecision="8" numLines="239" samplesPerLine="211" numFrameComponents="3"&gt;
          &lt;componentSpec componentId="1" HsamplingFactor="2" VsamplingFactor="2" QtableSelector="0"/&gt;
          &lt;componentSpec componentId="2" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/&gt;
          &lt;componentSpec componentId="3" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/&gt;
      &lt;/sof&gt;
      &lt;dht&gt;
          &lt;dhtable class="0" htableId="0"/&gt;
      &lt;/dht&gt;
      &lt;dht&gt;
          &lt;dhtable class="1" htableId="0"/&gt;
      &lt;/dht&gt;
      &lt;dht&gt;
          &lt;dhtable class="0" htableId="1"/&gt;
      &lt;/dht&gt;
      &lt;dht&gt;
          &lt;dhtable class="1" htableId="1"/&gt;
      &lt;/dht&gt;
      &lt;sos numScanComponents="3" startSpectralSelection="0" endSpectralSelection="63" approxHigh="0" approxLow="0"&gt;
          &lt;scanComponentSpec componentSelector="1" dcHuffTable="0" acHuffTable="0"/&gt;
          &lt;scanComponentSpec componentSelector="2" dcHuffTable="1" acHuffTable="1"/&gt;
          &lt;scanComponentSpec componentSelector="3" dcHuffTable="1" acHuffTable="1"/&gt;
      &lt;/sos&gt;
     &lt;/markerSequence&gt;
  &lt;/javax_imageio_jpeg_image_1.0&gt;
  Format name: javax_imageio_1.0
  &lt;javax_imageio_1.0&gt;
  &lt;Chroma&gt;
      &lt;ColorSpaceType name="YCbCr"/&gt;
      &lt;NumChannels value="3"/&gt;
  &lt;/Chroma&gt;
  &lt;Compression&gt;
      &lt;CompressionTypeName value="JPEG"/&gt;
      &lt;Lossless value="FALSE"/&gt;
      &lt;NumProgressiveScans value="1"/&gt;
  &lt;/Compression&gt;
  &lt;Dimension&gt;
      &lt;PixelAspectRatio value="1.0"/&gt;
      &lt;ImageOrientation value="normal"/&gt;
      &lt;HorizontalPixelSize value="0.26458332"/&gt;
      &lt;VerticalPixelSize value="0.26458332"/&gt;
  &lt;/Dimension&gt;
  &lt;/javax_imageio_1.0&gt;
  </code></pre>