Objective: The objective of this post is give overview on how to generate a QR and Bar codes using ZXing API’s in Java. Also it gives an overview on how to decode a QR and Bar code in Java.
GitHub Source:
https://github.com/agilerules/IOT/tree/master/QRCode
Overview about QR Codes:
QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to efficiently store data; extensions may also be used.
The QR code system became popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes. Applications include product tracking, item identification, time tracking, document management, and general marketing.
A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera, and processed using Reed–Solomon error correction until the image can be appropriately interpreted. The required data are then extracted from patterns that are present in both horizontal and vertical components of the image.
Zxing:
ZXing ("Zebra Crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.
Supported Formats:
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
| ||
RSS14
| ||
RSS-Expanded
|
Besides the Java version, developers can leverage other ported projects such as QZXing, zxing-cpp, zxing_cpp.rb, python-zxing and ZXing .NET to quickly make barcode reader or writer software.
Task Details:
The following are the steps to run the program in your local environment.
1. Download the code from the GitHub and import the project QRCode as Maven Project into Eclipse (or any other Java editor). The maven will try to download all the dependent libraries like zxing’s core, javase jar files.
QR Generator:
2. If you run this program QRCodeGenerator.java, it is going to create a QR image (C:/QR/QRGenerator.png) with embossed QR Text pointing to the blog site http://agilerule.blogspot.in.
String codeText = "http://agilerule.blogspot.in";
String fileLocation = "C:/QR/QRGenerator.png";
String fileType = "png";
If you would like the modify them, you can change the following values in the Java code. If you would like to run the program as-is, please create a folder named “QR” in C drive (as this program is going to create the QRGenerator.png in the path C:\QR\.
3. If you would like to generate the QR Code then run the QRCodeGenerator.java in standalone way i.e If you are using Eclipse, then right click on the QRCodeGenerator.javaàRun Asà Java Application.
4. Once the program is successfully run then you will see the following message in the console:
You have successfully created QR Code with the image created at the location: C:/QR/QRGenerator.png
5. Now you should see the QR image file QRGenerator.png file in the C:\QR folder, which will look something like this.
QR Decoder:
6. If you run this program QRCodeDecoder.java, it will decode the QR image that is available in the path (C:/QR/QRGenerator.png) and prints the decoded text message. You can change the following path, just incase if you want.
File imageFile = new File("C:/QR/QRGenerator.png");
7. If you would like to decode the QR Code then run the QRCodeDecoder.java in standalone way i.e If you are using Eclipse, then right click on the QRCodeDecoder.javaàRun Asà Java Application.
8. Once the program is successfully run then you will see the following message in the console:
Barcode text: http://agilerule.blogspot.in
BarCode Generator:
9. If you run this program BarCodeGenerator.java, it is going to create a Bar code image (C:/QR/ CODE_128.png) in CODE_128 format and PDF_147 format with embossed Barcode Text pointing to the blog site http://agilerule.blogspot.in. If you would like the modify them, you can change the following values in the Java code.
// Write Barcode in CODE_128 format
String codeText = "http://agilerule.blogspot.in";
String fileLocation = "C:/QR/CODE_128.png";
// Write Barcode in PDF_147 format
codeText = "http://agilerule.blogspot.in";
fileLocation = "C:/QR/PDF_147.png";
If you would like to run the program as-is, please create a folder named “QR” in C drive (as this program is going to create the CODE_128.png and PDF_147.png in the path C:\QR\.
10. If you would like to generate the Barcode in CODE_128 and PDF_147 format then run the BarCodeGenerator.java in standalone way i.e If you are using Eclipse, then right click on the BarCodeGenerator.javaàRun Asà Java Application.
11. Once the program is successfully run then you will see the following message in the console:
You have successfully created Barcode 128 Code with the image created at the location: C:/QR/CODE_128.png
You have successfully created PDF147 Code with the image created at the location: C:/QR/PDF_147.png
12. Now you should see the QR image file CODE_128.png file and PDF_147.png in the C:\QR folder, which will look something like this.
CODE_128.png:
PDF_147.png
BarCode Decoder:
13. If you run this program BarCodeDecoder.java, it will decode the Barcode image that is available in the path (C:/QR/CODE_128.png and C:/QR/PDF_147.png) and prints the decoded text message. You can change the following path, just incase if you want.
String fileLocation = "C:/QR/CODE_128.png";
fileLocation = "C:/QR/PDF_147.png";
14. If you would like to decode the QR Code then run the QRCodeDecoder.java in standalone way i.e If you are using Eclipse, then right click on the QRCodeDecoder.javaàRun Asà Java Application.
15. Once the program is successfully run then you will see the following message in the console:
Barcode text: http://agilerule.blogspot.in
Java code:
QR Generator:
The following is the sample Java code using zxing API which will generate the QR Codes using QRCodeWriter():
QRCodeGenerator.java:
1. Define all the constants that are used in the code like QR Code Text, image file Location, Size, file Type (png or jpg), File Object etc. QR Code Text (here it is http://agilerule.blogspot.in) is the text you would emboss on your QR Code.
String codeText = "http://agilerule.blogspot.in";
String fileLocation = "C:/QR/QRGenerator.png";
int size = 250;
String fileType = "png";
File myFile = new File(fileLocation);
2. Define the EnumMap with the properties like Character Set, Margin, Error Correction that will be used as one of the input parameter for the QRCodeWriter API.
Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
3. Initialize the QRCodeWriter instance and invoke encode() method with the parameters like encode Text, BarCodeFormat for QR Code, size and map with EncodeHintTypes (like Character Set, Margin, Error Correction)
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(codeText, BarcodeFormat.QR_CODE, size, size, hintMap);
4. Now create the Graphics Image filled with WHITE and BLACK colour with respective height and width.
BufferedImage image = new BufferedImage(byteMatrixWidth, byteMatrixHeight, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, byteMatrixWidth, byteMatrixWidth);
graphics.setColor(Color.BLACK);
5. Now loop on the Width and Height and fill the rectangle block of WHITE and BLACK patches.
for (int i = 0; i < byteMatrixWidth; i++) {
for (int j = 0; j < byteMatrixHeight; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
6. Write the image using ImageIO.wirte() with parameters of graphics image (created in step 2, file type png, File object
ImageIO.write(image, fileType, myFile);
QR Code Decoder:
The following is the sample Java code using zxing API which will decode the QR Codes using decode() method of Reader object.
QRCodeDecoder.java
1. Read the image file path that you would like to decode using ImageIO.read() method
File imageFile = new File("C:/QR/QRGenerator.png");
BufferedImage image = ImageIO.read(imageFile);
2. Create the instance of BinaryBitmap of the image from LuminanceSource().
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
3. Create the Reader instance and invoke the decode() method on the Reader instance to get the Result and print the text using getText() method.
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
System.out.println("Barcode text: " + result.getText());
4. If the image is not in a proper format to decode then it will reach the catch block (in try-catch) and will print the value message in the console.
} catch (Exception e) {
System.out.println("The image is not in a recognizable QR format..");
}
Barcode Generator:
The following is the sample Java code using zxing API which will generate the Bar codes using Code128Writer ():
QRCodeGenerator.java:
1. Define all the constants that are used in the code like Bar Code Text, image file Location, Size. Barcode Code Text (here it is http://agilerule.blogspot.in) is the text you would emboss on your Barcode.
String codeText = "http://agilerule.blogspot.in";
String fileLocation = "C:/QR/CODE_128.png";
2. Define the EnumMap with the properties like Character Set, Margin, Error Correction that will be used as one of the input parameter for the QRCodeWriter API.
Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
3. Initialize the Code128Writer and PDF417Writer instance and invoke encode() method with the parameters like encode Text, BarCodeFormat for QR Code, size and map with EncodeHintTypes (like Character Set, Margin, Error Correction). Finally invoke the writeToStream() method of MatrixToImageWriter to create the image.
// Write Barcode in CODE_128 format
writer = new Code128Writer();
bitMatrix = writer.encode(codeText, BarcodeFormat.CODE_128, width, height, hintMap);
MatrixToImageWriter.writeToStream(bitMatrix, fileType, new FileOutputStream(new File(fileLocation)));
// Write Barcode in PDF_147 format
writer = new PDF417Writer();
bitMatrix = writer.encode(codeText, BarcodeFormat.PDF_417, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, fileType, new FileOutputStream(new File(fileLocation)));
BarCodeDecoder.java
The BarCodeDecoder is similar to that of the explanation provided in QRCodeDecoder.java section above.
This printer is astounding. I have utilized it. Moreover, i have a few encounters in 3d laser filtering. I fair adore this handle. Through this preparation, I can effectively degree my protest conjointly can discover the issues in it. If you would like a few quality benefits like this then visit 3d laser scanner Vancouver, BC. You'll be able to discover numerous things here. Quality 3d modeling services Denver, Colorado
ReplyDeleteWhether it is QRcode generate or one-dimensional barcode generate, this technology is very practical, and reverse one-dimensional or two-dimensional barcode read is also very convenient, suitable for developers' reference.
ReplyDeleteI am having trouble finding barcode scanner price in pakistan for my mini mart.
ReplyDeletePlease guide.
Thank you.
Rugged android handheld PDA
ReplyDeleteHi there,
ReplyDeleteThank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing Free Bitcoin QR Codes, BTC QR Code, BTC QR, BTC Address To QRCode, Bitcoin QR Maker, bitcoin qr generator with amount, bitcoin qr code generator blockchain Bitcoin QR CodeGenerator and our services are very fast.
Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for the informative post. Visit our site to know more. appsforlife
ReplyDeleteThanks for publishing the informative blog. Really a helpful blog.
ReplyDeleteERP for Engineering Industry
Engineering ERP Software
Thanks for sharing such an amazing content with us.
ReplyDeleteexport software
<a href="https://turboems.com/>export management software</a>
Hi, Thanks for sharing the amazing stuff I truly love your stuff about Scan barcode what an amazing peace of colaboration you have done for me.
ReplyDeleteMany thanks sir
Industrial Barcode Scanner PDA
ReplyDelete