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. : 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:
: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: :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:
: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: :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: : 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.
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:
Recommended Posts