Burn Annotations to an Image - Java

This tutorial shows how to burn annotations inside an AnnContainer onto an image in a Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers burning annotations to an image in a Java application.
Completion Time 30 minutes
Project Download tutorial project (1 MB)
Platform Java Application
IDE Eclipse / IntelliJ
Runtime 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 Burn Annotations to an Image - Java tutorial.

Create the Project and Add 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:

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 Burn Annotations Code

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

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

Java
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.annotations.engine.*; 
import leadtools.annotations.rendering.*; 

Inside the run() method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported.

Java
public static void main(String[] args) throws IOException 
{ 
	new _Main().run(args); 
} 
private void run(String[] args) { 
   try { 
	   Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
		Platform.loadLibrary(LTLibrary.LEADTOOLS); 
		Platform.loadLibrary(LTLibrary.CODECS); 
		Platform.loadLibrary(LTLibrary.DOCUMENT); 
		       
		SetLicense(); 
 
		burnAnnotationsToImage(); 
	}  
	catch(Exception ex) { 
		System.err.println(ex.getMessage()); 
		ex.printStackTrace(); 
	} 
} 

Add a new method to the _Main class called burnAnnotationsToImage and call it inside the run() method after the SetLicense() call, as shown above.

Add the code below to the burnAnnotationsToImage() method to load annotations from a file and burn them to the loaded image.

Java
void burnAnnotationsToImage() throws FileNotFoundException { 
        // Setting file paths 
        String imageFile = "Burn-Annotations-to-an-Image-Source-Image.jpg"; 
        String annFile = "Burn-Annotations-to-an-Image-Annotations-File.xml"; 
        String outputFile = "C:\\Temp\\BurnAnnOutput.jpg"; 
 
        // Creating engine and codecs 
        AnnJavaRenderingEngine _renderingEngine = new AnnJavaRenderingEngine(); 
        RasterCodecs codecs = new RasterCodecs(); 
        AnnCodecs annCodecs = new AnnCodecs(); 
        AnnContainer container = new AnnContainer(); 
 
        // Load image and annoations file 
        RasterImage srcImage = codecs.load(imageFile); 
        InputStream stream = new FileInputStream(annFile); 
        container = annCodecs.load(stream, 1); 
 
        // Burn annotations and save file 
        RasterImage burnImage = _renderingEngine.renderOnImage(container, srcImage); 
        codecs.save(burnImage, outputFile, RasterImageFormat.JPEG, 0); 
    } 

Run the Project

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

If the steps are followed correctly, the application loads the specified image as a RasterImage, loads the annotations contained in an XML file to an AnnContainer, and then burns those annotations to the image and exports that image to a file.

Wrap-up

This tutorial showed how to add the necessary references and code to burn annotations to an image. Also, it covered how to use the AnnCodecs, AnnContainer, and AnnJavaRenderingEngine classes.

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.