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 |
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.
In Eclipse, create a new Java project, and add the necessary LEADTOOLS references.
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>\LEADTOOLS21\Bin\Java
leadtools.jar
leadtools.annotations.engine.jar
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.
With the project created, the references added, and the license set, coding can begin.
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 main()
method after the SetLicense()
call, calling the load annotations method first.
Add the following import
statements to the import block at the top.
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import leadtools.*;
import leadtools.annotations.engine.*;
public static void main(String[] args) throws IOException
{
Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.CODECS);
SetLicense();
AnnCodecs annCodecs = new AnnCodecs();
AnnContainer _annContainer = LoadAnnotations(annCodecs);
SaveAnnotations(annCodecs, _annContainer);
}
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.
static 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 the below code inside the SaveAnnotations
method to add a new AnnObject to the AnnContainer, and export the AnnContainer to a new XML file.
static 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 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.
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.