Apache Tika - Extracting image file



Example - Extracting Content and Metadata from a image file

Given below is the program to extract content and metadata from a image file.

TikaDemo.java

package com.tutorialspoint.tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.image.JpegParser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;

public class TikaDemo {

   public static void main(final String[] args) throws IOException,SAXException, TikaException {

      //detecting the file type
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(new File("D:/projects/boy.jpg"));
      ParseContext pcontext = new ParseContext();
      
      //Jpeg Parse
      JpegParser  JpegParser = new JpegParser();
      JpegParser.parse(inputstream, handler, metadata,pcontext);
      System.out.println("Contents of the document:" + handler.toString());
      System.out.println("Metadata of the document:");
      String[] metadataNames = metadata.names();
      
      for(String name : metadataNames) { 		        
         System.out.println(name + ": " + metadata.get(name));
      }
   }
}

Output

Given below is the snapshot of Example.jpeg −

boy

The JPEG file has the following properties −

Property

After executing the program, you will get the following output.

Contents of the document:
Metadata of the document:
Resolution Units: inch
Number of Tables: 4 Huffman tables
File Modified Date: Tue Oct 28 11:33:31 +05:30 2025
Compression Type: Baseline
Data Precision: 8 bits
Number of Components: 3
tiff:ImageLength: 435
Component 2: Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Thumbnail Height Pixels: 0
Component 1: Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Image Height: 435 pixels
Thumbnail Width Pixels: 0
X Resolution: 96 dots
Image Width: 420 pixels
File Size: 40050 bytes
Component 3: Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
Version: 1.1
File Name: apache-tika-2559742186857137057.tmp
tiff:BitsPerSample: 8
tiff:ImageWidth: 420
Y Resolution: 96 dots
Advertisements