This tutorial shows how to create a Java application that detects and extracts barcodes.
Overview | |
---|---|
Summary | This tutorial covers how to extract barcodes 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 |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Load and Save an Image tutorials, before working on the Detect and Extract Barcodes - Java tutorial.
Saving images is not necessary to this tutorial, so saving portion can be commented out.
Start with a copy of the project created in the Load and Save an Image 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>\LEADTOOLS22\Bin\Java
leadtools.jar
leadtools.barcode.jar
leadtools.codecs.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, the license set, and the load image code added, coding can begin.
In the Package Explorer, open the _Main.java
class. Add the following import
statements to the import block at the top.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import leadtools.*;
import leadtools.barcode.*;
import leadtools.codecs.*;
Add a new method to the _Main
class called ReadBarcode(RasterImage _image)
and call it inside the main()
method after the LoadImage()
method call.
public static void main(String[] args) throws IOException
{
new _Main().run(args);
}
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.BARCODE);
Platform.loadLibrary(LTLibrary.CODECS);
SetLicense();
RasterCodecs codecs = new RasterCodecs();
String inputFile = "C:\\LEADTOOLS22\\Resources\\Images\\noisy_barcode.jpg";
//String outputFile = "C:\\Temp\\test.jpg";
RasterImage image = LoadImage(inputFile, codecs);
ReadBarcode(image);
// SaveImage(image, outputFile, codecs);
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Add the below code to the ReadBarcode
method to detect and output barcode information to the console.
void ReadBarcode(RasterImage _image)
{
BarcodeEngine bcEngine = new BarcodeEngine();
BarcodeData data = bcEngine.getReader().readBarcode(_image, LeadRect.getEmpty(), BarcodeSymbology.UNKNOWN);
if (data != null)
{
System.out.println("\nBarcode Found!" +
"\nBarcode Symbology: " + data.getSymbology() +
"\nBarcode Value: " + data.getValue() +
"\nBarcode Bounds: " + data.getBounds());
}
}
Note
There are a few other properties inside the BarcodeData class, this tutorial utilizes the most common properties.
Run the project by selecting Run -> Run.
If the steps were followed correctly, the application loads the barcode image, runs barcode recognition, and then outputs the barcode information to the console.
This tutorial uses this sample image.
This tutorial showed how to run barcode recognition on an image. Also, it covered how to use the BarcodeEngine
, BarcodeReader
, and BarcodeData
classes.