JAVA: Realizes generation and scanning of various barcodes

Barcodes are visualized, machine-readable data that usually describe information about what carries the barcode. Barcodes are widely used in fields such as product distribution, book management, postal management, and banking systems. This article describes common one-dimensional and two-dimensional bar code generation and scanning.

Required tools:

Below is a list of barcode types supported by the free version. 9.png : top: P.s. I want more barcode types. Please refer to Spire.Barcode for Java commercial version.

: one: Barcode generation:

Barcode generation covers two important types. One is Barceode Settings and the other is Barceode Generator. Barch ode Settings are specific types for customizing barcodes, such as data, size, and color. Barceode Generator creates image data based on Barcede Settings. The supported partial barcode generations in the table above are as follows:

:black_small_square:Codebar:


import com.spire.barcode.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class CODABAR {
    public static void main(String[] args) throws Exception {
	//Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
	//Set data
        settings.setData("2030405060");
	//Set the Symbology property
        settings.setType(BarCodeType.CODABAR);
	//Set Show Text location on bottom
        settings.setShowTextOnBottom(true);
	//Set border is visible
        settings.hasBorder(true);
	//Set the CodabarStartChar and CodebarStopChar
        settings.setCodabarStartChar(CodabarChar.B);
        settings.setCodabarStopChar(CodabarChar.D);
	//Create the BarcodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
	//Get image from the BarcodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
	//Save the image
        ImageIO.write(bufferedImage,"png",new File("CODABAR.png "));
    }
}

Execution effect: 1.png

:black_small_square:Code11:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_11 {
    public static void main(String[] args) throws IOException {

	//Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("12345-67890"); 
	//Set the Symbology property
        settings.setType(BarCodeType.CODE_11);
	//Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
	//Set Border is visible
        settings.hasBorder(true);
	//Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
	//Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
	//Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_11.png "));

    }
}

Execution effect: 2.png :black_small_square:Code 39:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_39 {
    public static void main(String[] args) throws IOException {
        //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("Bar 987654321");
        //Set the Symbology property
        settings.setType(BarCodeType.CODE_39);
        //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
        //Set Border is visible
        settings.hasBorder(true);
        //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_39.png "));

    }
}

Execution effect: 3.png

:black_small_square:Code 128:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_128 {
    public static void main(String[] args) throws IOException {
        //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("ABCD 12345 abcd");
        //Set the Symbology property
        settings.setType(BarCodeType.CODE_128);
        //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
        //Set Border is visible
        settings.hasBorder(true);
        //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_128.png "));

    }
}

Execution effect: 4.png :black_small_square:QR_Code:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QR_CODE {
    public static void main(String[] args) throws IOException {
	 //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
	 //Set Data
        settings.setData("ABC 123456789");
	 //Set the Symbology property
        settings.setType(BarCodeType.QR_CODE);
	 //Set the QR_CODE size
        settings.setX(2);
	 //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
	 //Set Border is visible
        settings.hasBorder(true);
	 //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
         //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
	 //Save the image
        ImageIO.write(bufferedImage,"png",new File("QR_CODE.png "));

    }
}

Execution effect: 5.png : two: Barcode scan:

This is an attempt to scan several barcode sets together and read multiple barcode data using the scan () method of Barcode Scanners. The image and code are as follows. 6.png

            public class Scan {
            public static void main(String[] args) throws Exception {

                  //Get code information by scanning the image   
             String[] s=BarcodeScanner.scan("C:\\Users\\Administrator\\Desktop\\Barcode.png ");
                  for (int i=0;i< s.length ;i++){
                 System.out.println(s[i]);
                     }
                   }
                 }

Execution effect: 7.png

Recommended Posts

JAVA: Realizes generation and scanning of various barcodes
Advantages and disadvantages of Java
Various methods of Java String class
About fastqc of Biocontainers and Java
[Java] Judgment of identity and equivalence
[Java] Various summaries attached to the heads of classes and members
After 3 months of Java and Spring training
[Java] Inheritance and structure of HttpServlet class
[Java / Swift] Comparison of Java Interface and Swift Protocol
Introduction of New Generation Java Programming Guide (Java 10)
Introduction of New Generation Java Programming Guide (Java 11)
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Introduction of New Generation Java Programming Guide (Java 12)
Discrimination of Enums in Java 7 and above
Create barcodes and QR codes in Java PDF
[Java] Personal summary of classes and methods (basic)
Parallel and parallel processing in various languages (Java edition)
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Various correspondence table of Spring Framework and Spring Boot
Basics of threads and Callable in Java [Beginner]
[Java] Classification memo of compilation error and run-time error
Introduction and introduction of management screen generation gem Administrate
Java enables extraction of PDF text and images
Java permutation generation
Java and JavaScript
XXE and Java
[Java] Overview of Java
[Java] Types of comments and how to write them
Summary of ToString behavior with Java and Groovy annotations
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] About Objects.equals () and Review of String comparisons (== and equals)
I summarized the types and basics of Java exceptions
List of frequently used Java instructions (for beginners and beginners)
Use of Abstract Class and Interface properly in Java
Equivalence comparison of Java wrapper classes and primitive types
[Java] [Kotlin] Generically call valueOf and values of Enum
[Java10] Be careful of using var and generics together
[Java] Handling of character strings (String class and StringBuilder class)