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>\LEADTOOLS21\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 main() 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 
{ 
   Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64"); 
   Platform.loadLibrary(LTLibrary.LEADTOOLS); 
   Platform.loadLibrary(LTLibrary.CODECS); 
   Platform.loadLibrary(LTLibrary.DOCUMENT); 
       
   SetLicense(); 
 
   burnAnnotationsToImage(); 
} 

Add a new method to the _Main class called burnAnnotationsToImage and call it inside the main() 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
static void burnAnnotationsToImage() throws FileNotFoundException  
{  
   String imageFile = "FILE PATH TO IMAGE FILE";  
   String annFile = "FILE PATH TO XML FILE CONTAINING ANNOTATIONS";  
   String outputFile = "FILE PATH TO OUTPUT TO";  
     
   AnnJavaRenderingEngine _renderingEngine = new AnnJavaRenderingEngine(); 
     
   RasterCodecs codecs = new RasterCodecs();  
   {  
      AnnCodecs annCodecs = new AnnCodecs();  
      AnnContainer container = new AnnContainer();  
      RasterImage srcImage = codecs.load(imageFile);  
      {  
         container.getMapper().mapResolutions(srcImage.getXResolution(), srcImage.getYResolution(), srcImage.getXResolution(), srcImage.getYResolution());  
         container.setSize(container.getMapper().sizeToContainerCoordinates(srcImage.getImageSize().toLeadSizeD()));  
         InputStream stream = new FileInputStream(annFile);  
         container = annCodecs.load(stream, 1);  
         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 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

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