Barcode4J is
an open source JAVA API which is used to generate flexible barcodes. Using Barcode4J one
can generate almost all type of barcode formats like Bar Code 11, Bar Code 39, Bar
Code 93, Bar Code 128, Bar Code 128A, and Bar Code 128B.
If you want to know details about Barcode4J, you
can find some details here. Below is a sample code demo that demonstrates how to generate barcode of
3 of 9 format (Bar-Code 39) and Bar-Code128.
MyBarCodeUtility.java
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImage;
import java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
org.krysalis.barcode4j.impl.code128.Code128Bean;
import
org.krysalis.barcode4j.impl.code39.Code39Bean;
import
org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import
org.krysalis.barcode4j.tools.UnitConv;
public class MyBarCodeUtility
{
private String barCodePath = "D:/";
public void createBarCode128(String fileName) {
try {
Code128Bean bean = new Code128Bean();
final int dpi = 160;
//Configure the barcode
generator
bean.setModuleWidth(UnitConv.in2mm(2.8f
/ dpi));
bean.doQuietZone(false);
//Open output file
File outputFile = new File(barCodePath + fileName + ".JPG");
FileOutputStream out = new
FileOutputStream(outputFile);
BitmapCanvasProvider canvas = new
BitmapCanvasProvider(
out, "image/x-png", dpi,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
bean.generateBarcode(canvas, fileName);
//Signal end of generation
canvas.finish();
System.out.println("Bar Code
is generated successfully…");
}
catch (IOException
ex) {
ex.printStackTrace();
}
}
public void createBarCode39(String fileName) {
try {
Code39Bean bean39 = new Code39Bean();
final int dpi = 160;
//Configure the barcode
generator
bean39.setModuleWidth(UnitConv.in2mm(2.8f
/ dpi));
bean39.doQuietZone(false);
//Open output file
File outputFile = new File(barCodePath + fileName + ".JPG");
FileOutputStream out = new
FileOutputStream(outputFile);
//Set up the canvas provider for
monochrome PNG output
BitmapCanvasProvider canvas = new
BitmapCanvasProvider(
out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
bean39.generateBarcode(canvas,
fileName);
//Signal end of generation
canvas.finish();
System.out.println("Bar Code
is generated successfully…");
}
catch (IOException
ex) {
ex.printStackTrace();
}
}
public static void main (String str[])
{
MyBarCodeUtility barCodeUtil = new
MyBarCodeUtility();
// This will generate Bar-Code 3 of 9
format
barCodeUtil.createBarCode39("naeemgik
- 12345");
// This will generate Bar-Code 128
format
barCodeUtil.createBarCode128("0123456789");
}
}
After executing the above code snippet two Bar Codes should generate at
mentioned path of your hard drive. It should look like:
Bar-Code 39 format
Bar-Code 128 format
Hello Said Naeem Shah,
ReplyDeleteThis post is really helpful and i am able to successfully generate the barcode in java. But the problem is with the jdk. My requirement is to generate a barcode from an AWT application which runs on JDK1.3 or Creme JVM (which supports upto JDK1.3). It does not generate barcode on JDK1.3 platform or doesn't show any exception though.
Please please help.
barcode4j works for JDK 1.4.x or higher. If you use JDK 1.3.x you will need to add XML support (JAXP, Xerces and Xalan). JDK 1.4 has XML support built-in. For detail refer to their official site FAQ
ReplyDeletehttp://barcode4j.sourceforge.net/faq.html
Its giving exception on the device that NoSuchMethodError because the creme jvm is not supporting some methods of BufferedImage, so i am facing this new issue now. Please help if you know something about this.
ReplyDeleteThanks for sharing this effective article. I like the Idea. Great thinking! There is wonderful about "Generating Barcode in Java using Barcode4J API". I am impressed by the quality of information on this website. There are a lot of good quality resources here.
ReplyDeleteI think, There are different types of bar code symbologies for different types of bar code applications.
I am sure I will visit this place again soon. You can find some information about code 128.
Thanks for your feedback. Its my pleasure that it helped you.
DeleteYes, As i mentioned at top there are various types of Barcode. In order to generate for other types barcodes, You just need to change this method: Code39Bean()
Thnx sir your post is so helpfull and valuable for me
ReplyDeletethank your code is able to genrate barcode i am using Ean 13 but barcode can not be read by scanner iball-LS392
ReplyDelete