How to Use Java Barcode Reader API

Last Updated on

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.There are app developers UK who can manage any kind of development process.

The Complete SDK Package

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

Java Barcode SDK

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:

 <repositories>
    <repository>
         <id>dbr </id>
         <url>https://download2.dynamsoft.com/maven/dbr/jar </url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.dynamsoft</groupId>
        <artifactId>dbr</artifactId>
        <version>7.5.0</version>
    </dependency>
</dependencies> 

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) {
                System.out.println(result.barcodeText);
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
        }

    }
}

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;
br.updateRuntimeSettings(runtimeSettings);

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, "");
inputStream.close();

Decoding Java OpenCV Mat

Install OpenCV Java via Maven:

<dependency>
  <groupId>org</groupId>
  <artifactId>opencv</artifactId>
  <version>4.3.0</version>
</dependency>

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 = ImageIO.read(new File(filename));
} catch (IOException e) {
    System.out.println(e);
    return;
}

Read barcodes from BufferedImage:

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

API Reference

https://www.dynamsoft.com/help/Barcode-Reader-Java/index.html

SDK License

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

Samples

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.

Subscribename@email.com