Dev Center
Table of contents

Getting Started with Java

In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using Java.

Requirements

  • Operating systems:
    • Windows 7, 8, 10, 11
    • Windows Server 2003, 2008, 2008 R2, 2012, 2019, 2022
    • Linux x64 (Ubuntu 14.04.4+ LTS, Debian 8+, etc.)
    • Linux arm 64bit
    • macOS x64: 10.12+
    • macOS ARM: 11+
  • Developing Environment:
    • JDK 1.7 and above

Note: Dynamsoft Barcode Reader provides both online and offline license options. The online license option might not work in an environment that doesn’t have network connection. In such case, you can get an offline trial license key via Customer Portal or by contacting us.

Installation

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.

Build Your First Application

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 here.

Create a New Project

  1. Open Eclipse. Go to File > New > Project, create a new Java project DBRJavaSample.

  2. Add a new Class named DBRJavaSample into the project.

Add the Library Reference

  1. Right click on Project -> Properties > Java Build Path > Libraries > Add external JARs, Browse to [INSTALLATION FOLDER]\lib and Select dynamsoft-barcodereader-{version number}.jar.

  2. Import the package in the file DBRJavaSample.java

    import com.dynamsoft.dbr.*;
    

Initialize a Barcode Reader Instance

  1. Initialize the license key.

     BarcodeReader.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.
  2. Create an instance of Dynamsoft Barcode Reader.

     BarcodeReader reader = BarcodeReader.getInstance();
     if(reader != null)
     {
         // Add your code here to call decoding method, process barcode results and so on
         // ...
         // Recycle the instance
         reader.recycle();
     }
    

Configure the Barcode Scanning Behavior (OPTIONAL)

  1. Set barcode format and count to read.

     PublicRuntimeSettings runtimeSettings = reader.getRuntimeSettings();
     runtimeSettings.barcodeFormatIds = EnumBarcodeFormat.BF_ONED | EnumBarcodeFormat.BF_QR_CODE;
     runtimeSettings.barcodeFormatIds_2 = EnumBarcodeFormat_2.BF2_POSTALCODE | EnumBarcodeFormat_2.BF2_DOTCODE;
     runtimeSettings.expectedBarcodesCount = 10;
     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.

    The Barcode Reader SDK comes with a large array of runtime settings to optimize the performance of the library. To learn about all the runtime settings, please visit the RuntimeSettings API page. To learn more about the cases and situations in which the settings can help, please visit the Explore Features page.

Decode and Output Results

  1. Decode barcodes from an image file.
  2. Get and output barcode results.

    try
    {
        TextResult[] results = reader.decodeFile("[INSTALLATION FOLDER]/images/AllSupportedBarcodeTypes.png", "");
        if (results != null && results.length > 0) {
             System.out.println("Total barcodes found: " + results.length);
             for (int iIndex = 0; iIndex < results.length; iIndex++)
             {
                 System.out.println("Barcode " + (iIndex + 1));
                 System.out.println("    Barcode Format: " + results[iIndex].barcodeFormatString);
                 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.

Release Resource

  1. Destroy the instance to release all resources.

     if(reader != null)
     {
         reader.recycle();
     }
    

Note:
Please change all [INSTALLATION FOLDER] in above code snippet to your unpacking path.

Build and Run the Project

  1. Right click the project, click Run As > Java Application.

You can download the entire source code here.

Next Steps

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 10.x
    • Version 10.2.0
    • Version 10.0.20
    • Version 10.0.10
    • Version 10.0.0
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.30
    • Version 9.6.20
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.0
    • Version 9.0.0
  • Version 8.x
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +