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
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();
}
}
}

Read more...
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();
}
}
}

Read more...





