Load, Add, and Save Annotations - Java

This tutorial shows how to use the LEADTOOLS SDK to create a Java application that loads, saves, and splits annotation containers.

Overview  
Summary This tutorial covers how to load, add, and save annotations in a Java application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
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 Load, Add, and Save Annotations - 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. The following JAR files are needed for this tutorial:

The JAR files are located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java

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

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

Add the following import 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.annotations.engine.*; 

In the Package Explorer, open the _Main.java class. Add two new methods to the _Main class called LoadAnnotations(AnnCodecs _annCodecs) and SaveAnnotations(AnnCodecs _annCodecs, AnnContainer _annContainer). Call these methods inside the run() method after the SetLicense() call, calling the load annotations method first.

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); 
			 
		SetLicense(); 
			 
		AnnCodecs annCodecs = new AnnCodecs(); 
		AnnContainer _annContainer = LoadAnnotations(annCodecs); 
		SaveAnnotations(annCodecs, _annContainer); 
	} catch(Exception ex) { 
		System.err.println(ex.getMessage()); 
		ex.printStackTrace(); 
	} 
} 

Add the below code to load the annotations from an XML file. The code assumes an annotations file already exists in the C:\Temp folder. In order to test it, this sample annotations file can be used.

Java
AnnContainer LoadAnnotations(AnnCodecs _annCodecs) throws FileNotFoundException { 
	InputStream stream = new FileInputStream("C:\\Temp\\test2.xml"); 
	// Load annotations 
	AnnContainer _annContainer = _annCodecs.load(stream, 1); 
		 
	// Print all the objects in the container 
	System.out.println("\n" + _annContainer.getChildren().size() +" Annotations Loaded:\n"); 
		 
	for (AnnObject object : _annContainer.getChildren()) { 
		System.out.println("Annotation: " + object.getFriendlyName().toString()); 
	} 
	return _annContainer; 
} 

Add and Save Annotations Code

Add the below code inside the SaveAnnotations method to add a new AnnObject to the AnnContainer, and export the AnnContainer to a new XML file.

Java
void SaveAnnotations(AnnCodecs _annCodecs, AnnContainer _annContainer) throws IOException { 
	OutputStream outStream = new FileOutputStream("C:\\Temp\\annotationsXML.xml"); 
		  
	// Add new rectangle annotation to container 
	AnnRectangleObject annRect = new AnnRectangleObject(); 
	annRect.setRect(new LeadRectD(100, 200, 400, 400)); 
	annRect.setStroke(AnnStroke.create(AnnSolidColorBrush.create("Red"), LeadLengthD.create(3.0))); 
	_annContainer.getChildren().add(0, annRect); 
	System.out.println("\nNew AnnRectangleObject added to annotation container\n"); 
		 
	// Export annotations to XML file 
	_annCodecs.save(outStream, _annContainer, AnnFormat.ANNOTATIONS, null, 0); 
	System.out.println(_annContainer.getChildren().size() + " Annotations saved to file"); 
} 

Run the Project

Run the project by selecting Run -> Run.

If the steps were followed correctly, the application loads the AnnContainer from the specified XML file, adds a new AnnRectangleObject to the AnnContainer, and then saves the modified container to a new XML file.

Java console output display screenshot

Wrap-up

This tutorial showed how to load and save annotations containers, as well as add an AnnObject to the AnnContainer. Also, showed how to use the AnnCodecs and AnnContainer 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.