Generate QR Code in Java

QR (Example)

QR (Example)

Just snippet code to generate QR Code in Java, this is Maven Dependency :

<dependency>
	<groupId>net.glxn</groupId>
	<artifactId>qrgen</artifactId>
	<version>1.4</version>
</dependency>
<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>javase</artifactId>
	<version>3.2.0</version>
</dependency>
<dependency>
	<groupId>com.google.zxing</groupId>
	<artifactId>core</artifactId>
	<version>3.2.0</version>
</dependency>

and this is the java code (Written in Spring MVC Framework) :

        @RequestMapping(value = "/qr", method = RequestMethod.GET)
	public String generateQr(Model model, HttpServletResponse response){
		try {
			String qrText = "http://blog.hidupbersahaja.com";
			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();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "index";
	}

If you don’t know how to create Spring MVC Application, you can read my post on Spring 4 MVC Hello INDONESIA. And the result :

QR Code Result

QR Code Result

Good Luck .. !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>