Generating EAN-8 standard barcode using Barcode4J

14 / Apr / 2011 by Vishal Sahu 2 comments

Hi,

In one of my project i needed to generate EAN-8 standard bar-code for identifying the various products. I searched for various libraries available for generating bar-code and found a library to do so. The library i used for generating the bar-code is Barcode4J.
You can download it from here

Barcode4J is a flexible generator for barcodes written in Java. It’s free, available under the Apache License, version 2.0.
The bar-code generated with the help of of this library uses eight digit number hence EAN-8.

To generate bar-code image from this library is quite simple. Just put the barcode4j.jar in the lib folder of the application.


Code to generate image is:-

[java]
public File generateBarcode(Long codeDigits){
try {
//’codeDigits’ is the code for which we want to generate bar-code image
EAN8Bean bean = new EAN8Bean();
final int dpi = 150;
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
bean.setFontSize(2.0)
bean.doQuietZone(true)
File outputFile = new File(filepath) // existing file in the file system
OutputStream out = new FileOutputStream(outputFile)

// class to convert provided image into barcode image
BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0)
bean.generateBarcode(canvas, codeDigits)
canvas.finish()
return outputFile
}

[/java]

So, here it takes a file from the file system and convert it into the required barcode image. Now, the user can simply display this image on the items.

In my case, i needed to generate a bar-code image for each product, so i create new file for every item, and stored it on the file system with unique name.

This works in my case.
Hope it helps


Cheers..!!!
Vishal Sahu
vishal@intelligrape.com

FOUND THIS USEFUL? SHARE IT

Tag -

barcode

comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *