How to create barcode using barbecue
Barbecue is an open-source, Java, barcode drawing library. Best of all
- it's completely
FREE! You can use, and extend, Barbecue for whatever reasons you like,
and distribute it in your application, at no cost.
Barbecue has been written in an open ended way in order to enable the
easy addition of new barcode formats without having to changes the underlying
API for the user. Currently, Barbecue supports the following barcode
formats:
- Code 128 (full implementation)
- EAN/UCC 128 (full implementation)
- Code 39 (full implementation)
- Codabar (full implementation)
- PDF417 (experimental implementation)
Barbecue will only work with Java 1.3 and above.
Barbecue can be used both as a Swing/AWT component, and as a standalone "drawable" object
that
can paint itself straight onto a Graphics(2D) - this is useful for printing
barcodes in Java.
Take a look at the example Java code in the examples directory for more
information on how to
use Barbecue in code. The API documentation is also included in this
release and is available
at barbecue.sourceforge.net.
Barbecue also includes a barcode generating servlet (BarcodeServlet).
It requires a "data"
parameter that specifies the data to encode. Please see the Javadocs
and the examples folder
for more information on optional parameters and using the servlet.
Try the following sample code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.sourceforge.barbecue.*;
public class Barcode_Demo extends JFrame {
public Barcode_Demo() {
initComponents();
}
private void initComponents() {
setTitle("Barcode Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
toolbar = new JToolBar();
inputBarcode = new JTextField();
okButton = new JButton();
panel = new JPanel();
toolbar.setFloatable(false);
toolbar.add(inputBarcode);
okButton.setText("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
toolbar.add(okButton);
getContentPane().add(toolbar, BorderLayout.NORTH);
getContentPane().add(panel, BorderLayout.CENTER);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
getRootPane().setDefaultButton(okButton);
}
private void okButtonActionPerformed(ActionEvent evt) {
try {
barcode = BarcodeFactory.createCode128B(inputBarcode.getText());
} catch (BarcodeException e) {
e.printStackTrace();
}
panel.add(barcode);
validate();
}
public static void main(String args[]) {
new Barcode_Demo().setVisible(true);
}
private JButton okButton;
private JPanel panel;
private JTextField inputBarcode;
private JToolBar toolbar;
private Barcode barcode;
}
You should be able see the following result:
|