DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

4

Generate and Read Barcode in Java

A barcode is a visual, machine-readable representation of data; the data usually describes something about the object that carries the barcode. Barcodes have been widely used in many fields such as commodity circulation, book management, postal management, banking system, etc.

In this blog post, I am going to introduce how to generate and read some popular 1D and 2D barcodes using Free Spire.Barcode for Java. Below is a list of barcode types supported by the free version.

Barcode Types Generate Read
CODABAR
CODE_11
CODE_39
CODE_39_EXTENDED
CODE_93
CODE_93_EXTENDED
CODE_128
EAN_8
EAN_13
EAN_128 ×
EAN_14
SCC_14
POST_NET ×
QR_CODE ×

For more barcode types, try the commercial version of Spire.Barcode.

Generate Barcode Image

There are two important classes involved in generating barcode, one is BarcodeSettings, the other is BarcodeGenerator. BarcodeSettings is used to customize your barcode with the specific type, data, size, color, etc. BarcodeGenerator is used to create image data based on the barcode settings.

//create an instance of BarcodeSetteings
BarcodeSettings settings = new BarcodeSettings();
//set barcode type
settings.setType(BarCodeType.CODE_39);
//set barcode data
settings.setData("ZXC98-HK");
//set the display text
settings.setData2D("ZXC98-HK");
//show text on bottom
settings.setShowTextOnBottom(true);
//set the border invisible
settings.hasBorder(false);
//create BarCodeGenerator object based on settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
//generate image data 
BufferedImage bufferedImage = barCodeGenerator.generateImage();
//write image data to a .png format file
ImageIO.write(bufferedImage, "png", new File("Code39.png"));
Enter fullscreen mode Exit fullscreen mode

Output:
Format text content

Read Barcode from Image

To read the barcode image generated above, use scanOne() method of BarcodeScanner class.

String data = BarcodeScanner.scanOne("G:\\idea-projects\\spire.barcode samples\\Code39.png");
System.out.print(data);
Enter fullscreen mode Exit fullscreen mode

If your image has more than one barcodes, use scan() method instead to return scan results in a string array.

String[] data = BarcodeScanner.scan(string imgPath);
Enter fullscreen mode Exit fullscreen mode

Output:

Format text content

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay