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 |
|
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.
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:
leadtools.jar
leadtools.annotations.engine.jar
leadtools.annotations.rendering.java.jar
leadtools.codecs.jar
leadtools.drawing.jar
For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application
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.
Open the _Main.java
class in the Package Explorer. Add the following 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.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.
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.
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 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.
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.