Ask Your Question
3

How can Barcode4j library be used in Java Swing to create a GS1-128 Barcode?

asked 2021-12-31 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-06-17 01:00:00 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-31 11:00:00 +0000

Seen: 8 times

Last updated: Jun 17 '22