Generate Code128, QRCode, PDF417 Barcode in Java

Generate Code128, QRCode, PDF417 Barcode in Java:

A barcode is an optical machine-readable representation of data. The data which it represents, can be get back by machine reading it. Originally barcodes represented data by varying the widths and spacing of parallel lines, and may be referred to as linear or 1D barcode. Today there are number of barcodes used for many a purposes.

In Java, we may need to generate a barcode for some particular string. The generated barcode can be used for printing labels etc. Following is an example of generating barcode from java using ZXing api of google. It is open source api which can be used for generating barcodes for several types. Following is simple java program to generate various barcode(code128, QRCode and pdf417(Mostly used on drivers license in USA)) using ZXing.


package barcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Writer;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.pdf417.encoder.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.File;
import java.io.FileOutputStream;

/**
*
* @author Yusata Infotech
*/
public class Barcode {
public static void main(String[] args) {
BitMatrix bitMatrix;
Writer writer = new QRCodeWriter();
try {
// Write Barcode
bitMatrix = new Code128Writer().encode("123456789", BarcodeFormat.CODE_128, 150, 80, null);
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("D://code128_123456789.png")));
System.out.println("Code128 Barcode Generated.");
// Write QR Code
bitMatrix = writer.encode("123456789", BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("D://qrcode_123456789.png")));
System.out.println("QR Code Generated.");
// Write PDF417
writer = new PDF417Writer();
bitMatrix = writer.encode("123456789", BarcodeFormat.PDF_417, 80, 150);
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("D://pdf417_123456789.png")));
System.out.println("PDF417 Code Generated.");
} catch (Exception e) {
System.out.println("Exception Found." + e.getMessage());
}

}
}

QRCode



Code128



PDF417

Download ZXing Library here

0 comments:

Post a Comment