Extract Image Information - Java

This tutorial shows how to create a Java application that uses the CodecsImageInfo class to get information about the various image files supported by LEADTOOLS.

Overview  
Summary This tutorial covers how to use the CodecsImageInfo Class in a Java application.
Completion Time 30 minutes
Eclipse Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Extract Image Information - Java tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java.

For this project, the following references are needed:

This tutorial uses LEADTOOLS Codec library support. For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Get Image Information Code

With the project created, the references added, and the license set, coding can begin.

Open the _Main.java class in the Project Explorer. Add the following statements to the import block at the top.

Java
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 

Add a new method called RasterCodecsImageInfo(String fileName). Call the new method below SetLicense(); inside the run() method, as shown below.

Java
private void run(String[] args) { 
		try { 
			Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
			Platform.loadLibrary(LTLibrary.LEADTOOLS); 
			Platform.loadLibrary(LTLibrary.CODECS); 
 
			SetLicense(); 
             
            String fileName = "C:\\LEADTOOLS23\\Resources\\Images\\image1.cmp"; 
			RasterCodecsImageInfo(fileName); 
		} catch (Exception ex) { 
			System.err.println(ex.getMessage()); 
			ex.printStackTrace(); 
		} 
	} 

Add the below CodecsImageInfo code inside the new method. The method's parameter will be the file path to the image from which the information is to be gathered from. For this tutorial, this sample image will be used.

Java
void RasterCodecsImageInfo(String fileName) { 
    RasterCodecs codecs = new RasterCodecs(); 
    CodecsImageInfo info = codecs.getInformation(fileName, true); 
    String inputFileName = fileName.substring(0, fileName.lastIndexOf(".")); 
    String codecsInfoString = ("Image Format: " + info.getFormat() + "\n" + 
            "Information for: " + inputFileName + "\n" + 
            "BitsPerPixel: " + info.getBitsPerPixel() + "\n" + 
            "BytesPerLine: " + info.getBytesPerLine() + "\n" + 
            "ColorSpace: " + info.getColorSpace() + "\n" + 
            "Byte Order: " + info.getOrder() + "\n" + 
            "Image Height: " + info.getHeight() + "\n" + 
            "Image Width: " + info.getWidth() + "\n" + 
            "Image X Resolution: " + info.getXResolution() + "\n" + 
            "Image Y Resolution: " + info.getYResolution() + "\n" + 
            "Compression: " + info.getCompression() + "\n" + 
            "Page Number: " + info.getPageNumber() + "\n" + 
            "Total Pages: " + info.getTotalPages()); 
 
    System.out.println(codecsInfoString); 
    codecs.dispose(); 
} 

Note: There are more properties inside the CodecsImageInfo class. The snippet above showcases the most commonly used properties.

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application runs and the console displays the file's information.

The application runs and the console displays the extracted information

Wrap-up

This tutorial showed how to use the CodecsImageInfo class.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.