Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are the steps to use the Barcode4j library in Java Swing to create a GS1-128 barcode:

  1. Add the Barcode4j jar file to your project's classpath.

  2. 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
    
  3. Set the data to be encoded in the barcode:

    code128.setCode("1234567890");
    
  4. 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.

  5. 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.