tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Barcodes > Barcode4j > Barcode EAN 128

Barcode EAN 128 

Barcode4J is a free and flexible Java library for Barcode generation. This requires the libraries barcode4j.jar, avalon-framework-4.2.0.jar to be in classpath. The following example shows generating EAN 128 Barcode.

File Name  :  
com/bethecoder/tutorials/barcode4j/EAN128Test.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.barcode4j;

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

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.krysalis.barcode4j.BarcodeException;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

public class EAN128Test {

  /**
   @param args
   @throws BarcodeException 
   @throws ConfigurationException 
   @throws IOException 
   */
  public static void main(String[] argsthrows ConfigurationException, BarcodeException, IOException {

    BarcodeUtil util = BarcodeUtil.getInstance();
    BarcodeGenerator gen = util.createBarcodeGenerator(buildCfg("ean-128"));

    OutputStream fout = new FileOutputStream("ean-128.jpg");
    int resolution = 200;
    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
        fout, "image/jpeg", resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);

    gen.generateBarcode(canvas, "010123456789012815051231");
    canvas.finish();
  }

  private static Configuration buildCfg(String type) {
    DefaultConfiguration cfg = new DefaultConfiguration("barcode");

    //Bar code type
    DefaultConfiguration child = new DefaultConfiguration(type);
      cfg.addChild(child);
    
      //Human readable text position
      DefaultConfiguration attr = new DefaultConfiguration("human-readable");
      DefaultConfiguration subAttr = new DefaultConfiguration("placement");
        subAttr.setValue("bottom");
        attr.addChild(subAttr);
        
        child.addChild(attr);
    return cfg;
  }
}
   

It gives the following output,

EAN 128 Barcode



 
  


  
bl  br