Thanks to smartphones, QR codes are becoming mainstream and they are becoming more and more useful. From the shelters, product packaging, home improvement stores, cars to many websites, all in their own web pages integrated QR Code, let people quickly find them. As the number of smartphones is increasing, the use of QR codes is increasing exponentially.
Let's look at a brief overview of QR codes and how to build them in Java.
QR Code Introduction
The QR code (quick Response Fast Response code) is a type of matrix barcode (or QR code) that is first designed for the automotive industry. Thanks to its fast reading and large storage capacity, the QR code began to be popular outside the automotive industry. The pattern is made up of black squares arranged on a white background. Coded yes data can be one of four standard data (numbers, alphanumeric, byte/binary, Kanji), but can also be extended to achieve more data.
Toyota's subsidiary, Denso Wave, invented the QR code in 1994 to track vehicles on the production line. Since then, the QR code has become the most popular two-dimensional barcode literal translation. The QR code is designed to support high-speed decoding of content.
Implementation of the QR code in Java Hello World
Zebra Crossing (ZXing) is a great open source library that can be used to generate and parse QR codes in virtually all platforms (Android, Javase, IPhone, RIM, Symbian). However, if you just want to generate a simple QR code, it's not possible to use it.
Qrgen was developed on the basis of zxing, a library that made it a piece of cake to generate QR codes from Java. It needs to rely on zxing, so you need zxing and Qrgen jar packages when you generate patterns.
You will not find the jar file in the zxing download page. Must be compiled by the source code itself.
Qrgen jar packages can be downloaded via the official website.
Import them into Classpath and execute the following Java code:
package net.viralpatel.qrcode;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
public class Main {
public static void main(String[] args) {
ByteArrayOutputStream out = QRCode.from("Hello World").to(ImageType.PNG).stream();
try {
FileOutputStream fout = new FileOutputStream(new File(
"C:QR_Code.JPG"));
fout.write(out.toByteArray());
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// Do Logging
} catch (IOException e) {
// Do Logging
}
}
}
If you open this JPEG file and sweep it with your iphone or Android QR Code tool, you'll see a cool "Hello world":)
In addition to using the Qrgen API to generate data streams, we can also use the following API to create a QR code:
// get QR file from text using defaults
File file = QRCode.from("Hello World").file();
// get QR stream from text using defaults
ByteArrayOutputStream stream = QRCode.from("Hello World").stream();
// override the image type to be JPG
QRCode.from("Hello World").to(ImageType.JPG).file();
QRCode.from("Hello World").to(ImageType.JPG).stream();
// override image size to be 250x250
QRCode.from("Hello World").withSize(250, 250).file();
QRCode.from("Hello World").withSize(250, 250).stream();
// override size and image type
QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file();
QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream();
The most common use of QR codes is to bring traffic to a specific page or download page in your site. As a result, QR codes often encode URLs or Web site addresses, which users can scan via their mobile camera and open in their browser. The URL can be encoded directly in the QR code. In the Hello World example above, simply replace the string "Hello World" with the URL you want to encode. Here is the code snippet:
ByteArrayOutputStream out = QRCode.from("http://viralpatel.net").to(ImageType.PNG).stream();
The QR code in the servlet
Most of the time, you need to generate some QR codes dynamically on the website. We've seen how easy it is to generate QR codes in Java. Now, let's look at how to integrate the generated QR code into the Java servlet.
Here is a simple HTTP servlet that uses the Qrgen and zxing libraries to create a QR code. The content of the QR code can be provided by the user.
The index.jsp file contains a simple HTML form with an input box and a Submit button. The user can enter the text that he wants to use for encoding and submit it.
index.jsp
<form action="qrservlet" method="get">
<p>Enter Text to create QR Code</p>
<input name="qrtext" type="text">
<input value="Generate QR Code" type="submit">
</form>
Qrcodeservlet.java
package net.viralpatel.qrcodes;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 QRCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String qrtext = request.getParameter("qrtext");
ByteArrayOutputStream out = QRCode.from(qrtext).to(ImageType.PNG).stream();
response.setContentType("image/png");
response.setContentLength(out.size());
OutputStream outStream = response.getOutputStream();
outStream.write(out.toByteArray());
outStream.flush();
outStream.close();
}
}
<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>QR_Code_Servlet</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>QRCodeServlet</servlet-name>
<servlet-class>net.viralpatel.qrcodes.QRCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QRCodeServlet</servlet-name>
<url-pattern>/qrservlet</url-pattern>
</servlet-mapping>
</web-app>
Summarize
Generating a QR code in Java is not only easy, but also very convenient. Integrating this functionality into any existing Java application is a piece of cake! In this tutorial, we learned how to generate QR codes in Java and Servlets.
How to use Java, servlet to create QR code