Java barcode SDK example: command line, interface, network

Keywords: Programming Java Google Maven snapshot

Share this article ZXing and Dynamsoft Java Barcode SDK Use in three scenarios.

Java Barcode application

My test image contains various types of barcodes. Add ZXing and Dynamsoft Barcode Reader to Maven's profile:

<repositories>
        <repository>
            <id>dbr</id>
            <url>https://download.dynamsoft.com/maven/dbr/jar</url>
        </repository>
</repositories>
<dependencies>
        <dependency>
            <groupId>com.dynamsoft</groupId>
            <artifactId>dbr</artifactId>
            <version>7.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
          </dependency>
</dependencies>

command line

New Maven project:

mvn archetype:generate -DgroupId=com.java.barcode -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

To import the required barcode Library:

import com.dynamsoft.barcode.*;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.*;

Use ImageIO and BufferedImage to read in pictures:

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;
}

ZXing uses different classes to read multiple barcodes and single barcodes. Here is how to read multiple barcodes:

BinaryBitmap bitmap = null;
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
bitmap = new BinaryBitmap(new HybridBinarizer(source));
             
MultiFormatReader reader = new MultiFormatReader();  
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
try {
    Result[] zxingResults = multiReader.decodeMultiple(bitmap);
} catch (NotFoundException e) {
    e.printStackTrace();
}
pixels = null;
bitmap = null;

Using Dynamsoft Barcode Reader is relatively simple, and the interface can recognize multiple barcodes by default:

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

To facilitate operation, package all dependencies with Maven plug-in:

<build>
  <plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
  </plugins>
</build>

Compile and run the project:

mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/command-line-1.0-SNAPSHOT-jar-with-dependencies.jar com.java.barcode.App ..\images\AllSupportedBarcodeTypes.png

Operation result:

Interface

Based on the command line code, Swing is used to implement the interface. Import related classes:

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
 
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

Add components JTextArea, JButton, JFileChooser,JComboBox:

public App() {
    super(new BorderLayout());
     
    mFileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            ".png", "png");
    mFileChooser.setFileFilter(filter);
    mLoad = new JButton("Load File");
    mLoad.addActionListener(this);
     
    mSourceList = new JComboBox(new String[]{"ZXing", "Dynamsoft"});
    mSourceList.setSelectedIndex(0);
     
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(mSourceList);
    buttonPanel.add(mLoad);
    add(buttonPanel, BorderLayout.PAGE_START);
     
    mTextArea = new JTextArea();
    mTextArea.setSize(480, 480);
    JScrollPane sp = new JScrollPane(mTextArea); 
    add(sp, BorderLayout.CENTER);
}

Add button response:

public void actionPerformed(ActionEvent e) {
 
    int returnVal = mFileChooser.showOpenDialog(App.this);
 
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = mFileChooser.getSelectedFile();     
        String filename = file.toPath().toString();          
        if (mSourceList.getSelectedItem().toString().equals("Dynamsoft")) {
            TextResult[] results = decodefileDynamsoft(filename);
        }
        else {
            Result[] results = decodefileZXing(filename);
        }
    } 
}

Compile run:

mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/gui-1.0-SNAPSHOT-jar-with-dependencies.jar com.java.barcode.App

network

According to the Guide to getting started To create a simple web application. Add in pom.xml:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-core</artifactId>
    <version>1.1.45</version>
    <exclusions>
        <exclusion>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.45</version>
</dependency>

In this way, you can test the server-side interface through the swagger UI. Create the server code:

@RestController
public class BarcodeController {
 
    private DynamsoftBarcode mDynamsoftBarcode;
    private ZXingBarcode mZXingBarcode;
 
    @Autowired
    public BarcodeController(DynamsoftBarcode dynamsoft, ZXingBarcode zxing) 
    {
        mDynamsoftBarcode = dynamsoft;
        mZXingBarcode = zxing;
    }
 
    @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());
    }
 
    @PostMapping(value = "/api/zxing"
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE
            , produces = MediaType.APPLICATION_JSON_VALUE)
    public BarcodeResponse getZXing(@RequestPart MultipartFile file) throws Exception {
        return mZXingBarcode.decode(file.getOriginalFilename(), file.getInputStream());
    }
}

Compile run:

mvn clean install
java -jar target/web-1.0-SNAPSHOT.jar

open http://localhost:8080/swagger-ui.html Test interface.

Android development

To develop Android, add:

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "http://download.dynamsoft.com/maven/dbr/aar"
        }
    }
}
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.dynamsoft:dynamsoftbarcodereader:7.3.0'

Source code

https://github.com/yushulx/java-barcode-command-gui-web

Posted by chiprivers on Tue, 17 Mar 2020 10:55:42 -0700