How to Use Java Barcode Reader API

Java Barcode API

Last Updated on 2021-03-15

Dynamsoft’s Barcode Reader SDK is a cross-platform bar code detection and decoding library. With the barcode scanning SDK, developers could easily build Java barcode applications on Windows, Linux, and macOS.

The Complete SDK Package

Get 30-day free trial of Dynamsoft Barcode Reader SDK >

- Java Barcode API

Java Barcode Quick Start

A dynamsoft-barcodereader-7.5.jar file is located under the Dynamsoft Barcode Reader\lib folder. You can easily import the jar file to your Java IDE (e.g. Eclipse). If you are using a Maven project, to add the dependency, you need to edit the pom.xml file as follows:

         <id>dbr </id>
         <url> </url>

Here is the code snippet showing how to decode barcodes from an image file:

import com.dynamsoft.barcode.*;

public class App {
    public static void main(String[] args) {
        String filename = "<file path>";
        try {
            BarcodeReader reader = new BarcodeReader(
                    "<Your trial license>");
            TextResult[] results = reader.decodeFile( filename, "");
            for (TextResult result : results) {
        } catch (Exception e) {
            // TODO: handle exception


Advanced API Usage

Loading Template Files and Updating Parameters

BarcodeReader br = new BarcodeReader("<LICENSE-KEY>");
br.initRuntimeSettingsWithFile("balanced.json", EnumConflictMode.CM_OVERWRITE); // Load a template file

PublicRuntimeSettings runtimeSettings = br.getRuntimeSettings();
runtimeSettings.barcodeFormatIds = EnumBarcodeFormat.BF_PDF417 | EnumBarcodeFormat.BF_QR_CODE; // Update barcode formats
runtimeSettings.barcodeFormatIds_2 = EnumBarcodeFormat_2.BF2_DOTCODE;

The template configuration is complicated. You can either customize the algorithm template by using the online barcode tool or download the existing template files from GitHub.

Decoding File Stream

You may use the Spring framework to build a web project and decode an uploaded image on the server-side. Assume you have following code to get the uploaded file from a multipart form:

@PostMapping(value = "/api/dynamsoft"
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE
            , produces = MediaType.APPLICATION_JSON_VALUE)
public BarcodeResponse getDynamsoft(@RequestPart MultipartFile file) throws Exception {
        return mDynamsoftBarcode.decode(file.getOriginalFilename(), file.getInputStream());

You can then decode barcodes from the file stream straightforwardly:

InputStream inputStream = file.getInputStream();
TextResult[] results = br.decodeFileInMemory(inputStream, "");

Decoding Java OpenCV Mat

Install OpenCV Java via Maven:


Capture a frame to OpenCV Mat and decode barcodes from Mat:

public static byte[] matToByteArray(Mat original)
    int width = original.width(), height = original.height(), channels = original.channels();
    byte[] sourcePixels = new byte[width * height * channels];
    original.get(0, 0, sourcePixels);
    return sourcePixels;
Mat frame = grabFrame();
TextResult[] results = reader.decodeBuffer(matToByteArray(frame), frame.width(), frame.height(), (int)frame.step1(), EnumImagePixelFormat.IPF_BGR_888, "");

Decoding BufferedImage

Read an image file to BufferedImage:

import java.awt.image.*;
import javax.imageio.ImageIO;
BufferedImage image = null;
try {
    image = File(filename));
} catch (IOException e) {

Read barcodes from BufferedImage:

TextResult[] results = null;
try {
    results = br.decodeBufferedImage(image, "");
} catch (Exception e) {
    System.out.println("decode buffered image: " + e);

API Reference

SDK License

To fully experience the functionalities of the SDK, please get a valid trial license.


Get more Java barcode scanning samples >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.