How to Generate and Read Barcode in Java

Barcode is the graphic representation of information mean you can hide your information in the image which can’t be read by human eye you can hide account information, product id, product information or something else you want. Creating and Reading barcode in java is long process and could be difficult if we start writing all code self. So there are API are available online which will help us to create and read bar code in java, for this purpose I used the most popular API called ZXing ( zebra crossing ). It supports all popular barcode formats as

1D product 1D industrial 2D
UPC-A Code 39 QR Code
UPC-E Code 93 Data Matrix
EAN-8 Code 128 Aztec (beta)
EAN-13 Codabar PDF 417 (beta)
ITF
RSS-14
RSS-Expanded

more information available here.

What is required?

First of all you have to download ZXing API and import them into eclipse project.

Here are the APIs you have to download.

core-2.2.jar

javase-2.2.jar

After download the above APIs, import them in to eclipse project. If you don’t know how to import .jar files into your eclipse’s project click here.

How to Create/Generate Barcode in Java:

First you have to create the instance of MultiFormatWriter which is convenience class to create or generate different Barcode on given format as Code128, QR_Code.

Now you can call the method encode() of MultiFormatWriter class which takes the following parameters.

String data = “1485-383-ABC”; // the data which you want to use to generate barcode.
BarcodeFormat.CODE_128 // type of the barcode can be changed as BarcodeFormat.QR_CODE
int width = 10; // width of the barcode/image
int height = 100; // height of the barcode/image

After creating the instance of MultiFormatWriter you need to call the encode() method which return BitMatrix object.

Now you have the Matrix of your data, you need to create the image from that data. So call the writeToFile() method from MatrixToImageWriter class. It takes the following parameters.

String format = “jpg” // the format of image.
String path = “QR_CODE.jpg”; // can be any name. bust must specify the same extension which you specify in format string as jpg.

Now let me show the complete code to generate the barcode in java.

 

How to Read Barcode in Java

Here you can read the barcode in java. I have describe the steps in the above code so similar process are here to read the Barcode in the java.