Generating barcode from Java using Maven

Barcode has become a standardway to identify articles throughout the world. Every day millions of barcodes are generated around the world. Here i would describe some basics of barcode standards and generation of barcode from java

Below are list of various barcode symbologies

1D barcode implementations
Interleaved 2 of 5
Code 39
Code 128
EAN-128, GS1-128 (based on Code 128)
Codabar
UPC-A and UPC-E (with supplementals)
EAN-13 and EAN-8 (with supplementals)
POSTNET
Royal Mail Customer Barcode (Four State)
USPS Intelligent Mail (4-State Customer Barcode)

2D barcode implementations
PDF 417 (ISO/IEC 15438:2001(E))
DataMatrix (ISO/IEC 16022:2000(E))

All the specifications surround around basic concepts of

  • Quiet Zone
  • Start Character
  • Encoded Data
  • Check Character
  • Stop Character
  • Quiet Zone

Read more about barcode generation technologies on wiki. I’ll explain more about generation of barcode in Java Using various open source Libraries and Integration with Maven

Barcode4j

/**
*
*/
package com.linkwithweb.barcode.barcode4j;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

/**
* @author kumara
*
*/
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
try {
// Create the barcode bean
Code39Bean bean = new Code39Bean();

final int dpi = 150;

// Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the narrow
// bar
// width exactly
// one pixel
bean.setWideFactor(3);
bean.doQuietZone(false);

// Open output file
File outputFile = new File("out.jpg");
OutputStream out = new FileOutputStream(outputFile);
try {
// Set up the canvas provider for monochrome JPEG output
BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
"image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY,
false, 0);

// Generate the barcode
bean.generateBarcode(canvas, "123456");

// Signal end of generation
canvas.finish();
} finally {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

Using barbecue

/**
*
*/
package com.linkwithweb.barcode.barbeque;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import net.sourceforge.barbecue.output.OutputException;

/**
* @author kumara
*
*/
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
try {
helloWorld.outputtingBarcodeAsPNG();
} catch (BarcodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void usingBarbecueAsSwingComponent() {
// Always get a Barcode from the BarcodeFactory
Barcode barcode = null;
try {
barcode = BarcodeFactory.createCode128B("My Barcode");
} catch (BarcodeException e) {
// Error handling
}
}

public void drawingBarcodeDirectToGraphics() throws BarcodeException,
OutputException {
// Always get a Barcode from the BarcodeFactory
Barcode barcode = BarcodeFactory.createCode128B("My Barcode");

// We are created an image from scratch here, but for printing in Java,
// your
// print renderer should have a Graphics internally anyway
BufferedImage image = new BufferedImage(500, 500,
BufferedImage.TYPE_BYTE_GRAY);
// We need to cast the Graphics from the Image to a Graphics2D - this is
// OK
Graphics2D g = (Graphics2D) image.getGraphics();

// Barcode supports a direct draw method to Graphics2D that lets you
// position the
// barcode on the canvas
barcode.draw(g, 10, 56);
}

public void outputtingBarcodeAsPNG() throws BarcodeException {
// get a Barcode from the BarcodeFactory
Barcode barcode = BarcodeFactory.createCode128B("My Barcode");

try {
File f = new File("mybarcode.png");

// Let the barcode image handler do the hard work
BarcodeImageHandler.savePNG(barcode, f);
} catch (Exception e) {
// Error handling here
}
}
}

and below is sample pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BarcodeGenerator</groupId>
<artifactId>com.linkwithweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BarcodeGenerator</name>
<description>BarcodeGenerator</description>
<dependencies>
<dependency>
<groupId>net.sourceforge.barbecue</groupId>
<artifactId>barbecue</artifactId>
<version>1.5-beta1</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>

Attached are sample images generated by code

Generated by Barbeque
Generated By Barbeque

Generated By barcode4j
Generated By barcode4j

Below is link for the sample working maven project
barcode

Rename .doc to .rar extract and njoy!!!!!!

Advertisements

6 thoughts on “Generating barcode from Java using Maven

  1. Your blog is really interesting to me and your topics are very relevant. I was browsing around and came across something you might find interesting. I was guilty of 3 of them with my sites. “99% of site owners are doing these 5 mistakes”. http://is.gd/8kynyd You will be suprised how easy they are to fix.

  2. Your site is pretty interesting to me and your subject matter is very relevant. I was browsing around and came across something you might find interesting. I was guilty of 3 of them with my sites. “99% of site managers are doing these five mistakes”. http://bit.ly/tSmwZA You will be suprised how fast they are to fix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s