java.net member

Rechercher dans ce site

Hello World, Barcode in Java, using ZXing

>> 29 September 2010

I have posted an article about using "Barbecue" for generating bar codes in Java, a few days ago.

 Yesterday I read this excellent post : about using "ZXing" Java Library (Open Source), to generate bar code in Java programs.

This library looks very interesting and powerful. Here is a Hello World.

ZXing ("Zebra Crossing")

The description is from the library's site
"ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java. Our focus is on using the built-in camera on mobile phones to photograph and decode barcodes on the device, without communicating with a server."

Project's Site :
http://code.google.com/p/zxing/

Installing
  • Download the current version (Zxing-1.6.zip)
  • Unzip in a folder of your choice, this will give "zxing-1.6" folder
  • For normal Java programs, two .jar files must be built
  • One of the possibilities is to use "ant"
  • cd zxing-1.6/core
  • Type "ant" you'll get in the same folder "core.jar"
  • cd zxing-1.6/javase
  • Type "ant" you'll get here "javase.jar"
  • In your Java project, create a folder (give it any name), for instance "lib"
  • Copy "core.jar" and "javase.jar" to YourProject/lib
  • Add these two .jar files to YourProject/properties/Java Build Path



Hello World

package com.java_javafx.ka;

import java.io.File;
import java.io.FileOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;

/**
 *
 * @author Kaesar ALNIJRES
 *
 */
public class HelloWorld {

  
    public static void main(String[] args) {
      
           int width = 440;
           int height = 48;
           
             
           BitMatrix bitMatrix;
        try {
            bitMatrix = new Code128Writer().encode("Hello World !!!",BarcodeFormat.CODE_128,width,height,null);
            MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("/home/kas/zxing_barcode.png")));
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


7 comments:

Anonymous,  February 14, 2013 at 5:10 AM  

I am new to using zxing and am getting a File Not Found exception when trying to create the new file saying the system cannot find the path specified, but it is a perfectly valid path (new FileOutputStream(new File("C:Users/owner/Desktop/zxing_barcode.png"). Could you offer any solutions?

okjavafx February 14, 2013 at 10:17 AM  

Hi,
This is not related to using ZXing, but mainly a problem to find your file. I have noticed that there is a missing slash after the drive name in your path "C:/Users/owner/Desktop/zxing_barcode.png". Please double check the path and the file is already there. Hope the answer is helpful.

Anonymous,  March 22, 2013 at 3:19 AM  

In response to above, it looks like you are on a windows machine and using forward slashes "/" instead of backslashes "\" like are using in windows. also, the above guide is using forward slashes, so im just guessing here, that its for a Linux distro, but i cant be sure about that part

okjavafx March 22, 2013 at 1:30 PM  

Hello,
No. All the tutors in this site are written and tested on Fedora, Linux (64bits). I only use the drive letter "C:" in the above response to give a full answer to the question. Thanx for the comment
Kaesar

Anonymous,  April 7, 2013 at 4:08 PM  

How to write something below the generated Barcode, imean to say some thing like this..
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hello World !!!

okjavafx April 12, 2013 at 9:57 AM  

Hello,
iText, does such things automatically
Kaesar

VILLI May 5, 2013 at 3:09 PM  

Thats pretty helpful, but I would like to know how to incorporate authentication of code into this. How would we compare the two codes, a generated one for verification, second one to be verified. What parameters or object values are requisite to be compared??

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP