Here are the steps to use the Barcode4j library in Java Swing to create a GS1-128 barcode:
Add the Barcode4j jar file to your project's classpath.
Create a new GS1-128 barcode object by using the following code:
Code128Bean code128 = new Code128Bean();
// Set the module width (1 to 5)
code128.setModuleWidth(UnitConv.inch2mm(0.006)); // 0.006 inches
// Set the barcode height (inches)
code128.setBarHeight(15);
// Set the quiet zone (inches)
code128.doQuietZone(true);
code128.setQuietZoneLeft(0); // 0 inches
code128.setQuietZoneRight(0); // 0 inches
Set the data to be encoded in the barcode:
code128.setCode("1234567890");
Generate the barcode image as a BufferedImage object:
BufferedImage bufferedImage = code128.createImageWithBarcode(graphics2D, Color.BLACK, Color.WHITE);
Note: graphics2D
should be a valid Graphics2D object.
Display the barcode image in a Swing component such as a JLabel or a JPanel:
ImageIcon imageIcon = new ImageIcon(bufferedImage);
JLabel label = new JLabel(imageIcon);
Note: You may need to resize the image to fit the component's size.
Asked: 2021-12-31 11:00:00 +0000
Seen: 8 times
Last updated: Jun 17 '22