Posts Tagged ‘QRCode’

Parsing QR Codes

Thursday, April 16th, 2009

For a class I am currently taking, we started to explore various ways of ensuring that a user has visited or is visiting a certain location, utilizing the common cellular phone. Our first thought of utilizing the phone’s geo-locating device, but quickly realized the amount of legal issues that would probably arise from such an endeavorer. We then thought of an alternate way: pictures! Most modern cellular phones have a camera built in and, worst possible case, a user surely has a camera of some sort at their disposal. Our idea consisted of utilizing QR Codes to ensure that a user had visited or is currently visiting a certain location.

Good Humorous Example of a QR Code

Good Humorous Example of a QR Code

QR Codes contain data in a 2D matrix, similar to our everyday barcodes which are simply 1D. Therefore, QR Codes can contain more data and simply look much cooler! Our plan is to simply encode a brief message into a QR Code and place it at said location for users to photograph and upload later if they do not have a camera-enabled phone, otherwise they would be able to take a picture of it and forward it to us via MMS message or e-mail.

The issue after this, however, was how to identify their QR Codes as being correct from a technology stand point. I figured that my current LAMP server would serve as an ideal testing grounds for this and I slowly was able to put together a sample QR Code parser. However, oddly enough, I was completely unable to discover a simple PHP QR Code decoder/parser. Instead of recoding the wheel per-say, I found an excellent Japanese QR Code decoder written in Java. Utilizing this, I was able to rig my PHP script up for excellent QR Code parsing! The fancy script I ended up with is below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
<html xmlns="">
	<title>QR Code Testing</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<div id="qrcode_text">
	if(isset($_FILES["qrcode"]) && isset($_FILES["qrcode"]["name"]) && strlen($_FILES["qrcode"]["name"]) > 0) {
		$img_file = mt_rand(0000, 9999) . $_FILES["qrcode"]["name"];
		if(move_uploaded_file($_FILES["qrcode"]["tmp_name"], "./temp/" . $img_file)) {
			$output = shell_exec("java -jar qrcode-cui.jar ./temp/" . $img_file . " 2>/dev/null");
			exec("rm -rf ./temp/" . $img_file);
			if(!$output || strlen($output) <= 0)
				echo "<b>Error</b><br/>It seems the QR Code was invalid or did not have any content";
				echo "<b>QR Code Content</b><br/>" . $output;
	} else if(isset($_POST["qrcode_url"])) {
			$qr_url = str_replace("`", "\`", str_replace("\\", "\\\\", str_replace(";", "", str_replace("&", "\&", $_POST["qrcode_url"]))));
			$output = shell_exec("java -jar qrcode-cui.jar \"" . $qr_url . "\" 2>/dev/null");
			if(!$output || strlen($output) <= 0)
				echo "<b>Error</b><br/>It seems the QR Code was invalid or did not have any content";
				echo "<b>QR Code Content</b><br/>" . $output;
	<div id="qrcode_form">
		<form action="#" method="post" enctype="multipart/form-data">
			<p style="display: none;"><input type="hidden" name="MAX_FILE_SIZE" value="2048000" /></p>
			<p><label for="qrcode">QR Code Image</label> <input type="file" id="qrcode" name="qrcode" /></p>
			<p><label for="qrcode_url">QR Code Image URL</label> <input type="text" id="qrcode_url" name="qrcode_url" /></p>
			<p><input type="submit" value="Check QR Code" /></p>
		<p>Don't know what a QR Code is? Here's an example!</p>
		<img src="./qrcode_test.png" alt="A Sample QR Code" width="268" height="268" />
		<p>QR Codes are 2D barcodes! They hold much more information than our common 1 dimensional barcodes</p>

So far, so good. If you have any questions or comments, feel free to comment below! You are also free to use the source code I have provided.