Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using Java.
If you haven’t downloaded the SDK yet, download the Java Package
now from Dynamsoft website and unpack the package into the directory of your choice.
For this tutorial, we unpack it to
[INSTALLATION FOLDER]
, change it to your unpacking path for the following content.
Let’s start by creating a console application which demonstrates how to use the minimum code to read barcodes from an image file.
You can download the entire source code and compiled program from Here.
Open Eclipse. Go to File > New > Project, create a new Java project DBRJavaSample
.
Add a new Class named DBRJavaSample
into the project.
Right click on Project -> Properties > Java Build Path > Libraries > Add external JARs, Browse to [INSTALLATION FOLDER]\lib
and Select dynamsoft-barcodereader-{version number}.jar
.
DBRJavaSample.java
import com.dynamsoft.dbr.*;
Create an instance of Dynamsoft Barcode Reader.
BarcodeReader reader = new BarcodeReader()
Initialize the license key.
reader.initLicense("<insert DBR license key here>");
Please replace
<insert DBR license key here>
with a valid DBR licensekey. There are two ways to obtain one:
- Search
initLicense
and find the license from[INSTALLATION FOLDER]/samples/BarcodeReaderDemo/src/com/dynamsoft/demo/BarcodeReaderDemo.java
.- Request a trial license from Customer Portal.
Set barcode format and count to read.
PublicRuntimeSettings runtimeSettings = reader.getRuntimeSettings();
runtimeSettings.barcodeFormatIds = EnumBarcodeFormat.BF_ALL;
runtimeSettings.barcodeFormatIds_2 = EnumBarcodeFormat_2.BF2_POSTALCODE | EnumBarcodeFormat_2.BF2_DOTCODE;
runtimeSettings.expectedBarcodesCount = 32;
reader.updateRuntimeSettings(runtimeSettings);
The barcode formats to enable is highly application-specific. We recommend that you only enable the barcode formats your application requires. Check out Barcode Format Enumeration for full supported barcode formats.
If you know exactly the barcode count you want to read, specify
expectedBarcodesCount
to speed up the process and improve the accuracy.
Get and output barcode results.
TextResult[] results;
try
{
results = reader.decodeFile("[INSTALLATION FOLDER]/images/AllSupportedBarcodeTypes.png", "");
System.out.println("Total barcodes found: " + results.length);
for (int iIndex = 0; iIndex < results.length; ++iIndex)
{
System.out.println("Barcode " + (iIndex + 1));
if (results[iIndex].barcodeFormat != 0)
{
System.out.println(" Barcode Format: " + results[iIndex].barcodeFormatString);
}
else
{
System.out.println(" Barcode Format: " + results[iIndex].barcodeFormatString_2);
}
System.out.println(" Barcode Text: " + results[iIndex].barcodeText);
}
}
catch (BarcodeReaderException exp)
{
System.out.println(exp.getMessage());
}
For the error handling mechanism, the SDK throws BarcodeReaderException for each function. You should add codes for exception handling based on your needs.
The SDK returns multiple barcode information, including barcode count, barcode format, barcode text, location, barcode raw data, etc. Check out TextResult for full supported result data.
Destroy the instance to release all resources.
reader.destroy();
Note:
Please change all[INSTALLATION FOLDER]
in above code snippet to your unpacking path.
You can download the entire source code and compiled program from Here.
latest version