QR code short for Quick Response Code is a two-dimensional matrix type barcode. Initially designed for the automotive industry has gained popularity in other areas such as retail packaging, storing web address and in smart phones due to its large storage capacity compared to standard UPC barcodes and fast readability.
Unlike the old barcode that was designed to be mechanically scanned by a narrow beam of light, the QR code is detected as a 2-dimensional digital image by a semiconductor image sensor and is then digitally analyzed by a programmed processor. The processor locates the three distinctive squares at the corners of the image, and normalizes image size, orientation, and angle of viewing, with the aid of a smaller square near the fourth corner. The small dots are then converted to binary numbers and validity checked with an error-correcting code. Nowadays there are scanner apps that uses the camera to scan these QR codes in our mobile phones and tablets.
The amount of data that can be stored in the QR Code symbol depends on the datatype
In this example we are going to learn how to generate a QR code from a String data. We take help of the open source library called ZXing((pronounced "zebra crossing") and QRGen that is built on top of Zxing library which makes it a breeze to generate the QR code in just a few lines of code. In your project you will need the following jar files
1
2
3
4
| Numeric only Max. 7,089 characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) Alphanumeric Max. 4,296 characters (0–9, A–Z [upper-case only], space, $, %, *, +, -, ., /, :) Binary/byte Max. 2,953 characters (8-bit bytes) (23624 bits) Kanji/Kana Max. 1,817 characters |
- core.jar
- javase.jar
- qrgen-1.1.jar
Application JSP - generateQRCode.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > < meta name = "robots" content = "noindex,nofollow" /> < title >Generate QR Code using QRGen and ZXing library</ title > < link rel = "stylesheet" href = "/resources/themes/master.css" type = "text/css" /> < link rel = "stylesheet" type = "text/css" /> < script type = "text/javascript" ></ script > < script type = "text/javascript" ></ script > < script type = "text/javascript" ></ script > < script src = "/resources/scripts/mysamplecode.js" type = "text/javascript" ></ script > < script type = "text/javascript" > $(document).ready(function() { $("#samplecode").validate({ rules: { qrText: "required" } }); }); </ script > </ head > < body > < div id = "allContent" > <%@include file="/header.jsp"%> <% String qrText = request.getParameter("qrText"); if(qrText == null){ qrText = ""; } %> < div id = "myContent" style = "width:50%;" > < form id = "samplecode" name = "samplecode" method = "POST" action = "generateQRCode.jsp" > < fieldset > < legend >< b > QR Code Generator - Request </ b ></ legend > < p > < label for = "qrText" > Input Text for QR Code </ label > < input id = "qrText" type = "text" name = "qrText" size = "50" value="<%= qrText %>" /> </ p > < input id = "generate" type = "submit" value = "Generate QR Code" /> </ fieldset > <% if (!qrText.trim().equalsIgnoreCase("")) { %> < fieldset > < legend >< b > QR Code Generator - Response </ b ></ legend > < img src = "../GenerateQRCode?qrText=<%= request.getParameter(" qrText") %>"> </ fieldset > <% } %> </ form > </ div > <%@include file="/footer.jsp"%> </ div > </ body > </ html > |
Source for Java Servlet - GenerateQRCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| package com.as400samplecode; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.glxn.qrgen.QRCode; import net.glxn.qrgen.image.ImageType; public class GenerateQRCode extends HttpServlet { private static final long serialVersionUID = 1L; public GenerateQRCode() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String qrText = request.getParameter( "qrText" ); ByteArrayOutputStream out = QRCode.from(qrText).to(ImageType.PNG).withSize( 300 , 300 ).stream(); response.setContentType( "image/png" ); response.setContentLength(out.size()); OutputStream os = response.getOutputStream(); os.write(out.toByteArray()); os.flush(); os.close(); } } |
You can choose to either create a file or ByteArrayOutputStream
1
2
| File file = QRCode.from( "Some input Text" ).file(); ByteArrayOutputStream stream = QRCode.from( "Some input Text" ).stream(); |
1
2
3
| QRCode.from( "Some input Text" ).to(ImageType.JPG).file(); QRCode.from( "Some input Text" ).to(ImageType.GIF).file(); QRCode.from( "Some input Text" ).to(ImageType.PNG).file(); |
1
| QRCode.from( "Some input Text" ).withSize( 200 , 200 ).file();
|
Great, luckily to see your article. I need such a sample of java code to generate qr code.
ReplyDeleteYour samples give me a guide. Thanks for sharing.