Using ZXing with Codenvy

Zebra Crossing (ZXing) is an awesome open source library that one can use to generate / parse QR Codes in almost all the platforms (Android, JavaSE, IPhone, RIM, Symbian etc). QRGen is a good library that creates a layer on top of ZXing and makes QR Code generation in Java a piece of cake. It has a dependency on ZXing, so you would need ZXing jar files along with QRGen to create QR Codes in Java.

On the download page of ZXing, you will not find the JAR files. Instead we have to create JAR files using the source code. (for non-maven users)

Create a Project

Login to your Codenvy workspace and create a new WAR Project say “serveQR”. Project file structure is shown below :
project

Create pom.xml and Add Dependencies

Specify the below dependencies in pom.xml which are prerequisite for the generating the required QR code. Make sure these jars are included under Maven Dependencies.

  • qrgen-1.3.jar
  • Javaee-web-api-6.0.jar

Pom.xml :

 <dependencies>
<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>java.net2</id>
<name>Repository hosting the jee6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>

Create Java Class

This servlet makes use of the qrgen.jar to process the given text through index.jsp and generates the QR code and also responsible for presenting the QR code as an Image.

Code Logic :

String qrtext = request.getParameter("txt");
		if(qrtext.length()!=0)
		{
			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();
		}
		else
		{
			PrintWriter pw = response.getWriter();
			pw.write("Required a valid string");
		}

Create index.jsp

Index jsp acts as an interface to input the text for which a QR Code would be generated by calling the servlet QRCodeGenServlet class .

<body>
<h2>
Create QR Code using zxing
</h2>
<form action="qrGen" method="get">
<p>Enter Text to generate QR Code</p>
<input type="text" name="txt" /><br/>
<input type="submit" value="Generate"/>
</form>
<br/>
</body>

qr

Create web.xml and success.jsp

We are mapping the index.jsp file to the earlier created servlet class in this web.xml file and the action will load the QR Image:

Web.xml :
<web-app>
<display-name>serveQR</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>QRCodeGenServlet</servlet-name>
<servlet-class>com.codenvy.QRCodeGenServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QRCodeGenServlet</servlet-name>
<url-pattern>/qrGen</url-pattern>
</servlet-mapping>
</web-app>

Build and Run the Application

Upon successful building the application and executing, it loads index.jsp where we input a text, then it will be successfully generates a QR Code and will presented with the QR Image.
qr_app