You are currently on IBM Systems Media’s archival website. Click here to view our new website.

IBM i > DEVELOPER > GENERAL

Real-World QR Codes

Real-World QR Codes

In my first two articles in this series, I showed how to create QR codes using RPG and Java. I even introduced new open-source tool QR4i and showed a few QR code examples. But what was lacking in those articles was a real-world example of how QR codes are being used within another medium (i.e., PDF, marketing piece, etc.). This article will dive into how QR codes could be used in the healthcare industry to create patient wristbands that have QR codes on them so hospital staff can easily scan them. You can see an example patient wristband shown in Figure 1.

If you have read my previous articles on TweetMe4i and QR4i, you’ll have learned how RPG can communicate with Java. I’ll be purposely glossing over those topics and instead describe the new things we encounter while addressing this real-world business need.

Using PDF With RPG

One way I’ve found to create printable documents on IBM i is to use the PDF specification. We could create our own implementation of the PDF specification within our RPG code, but that would be a fair amount of work. Instead, we’ll again reach out to the Java open-source community and utilize a free and open-source tool named iText. I’ve been using iText for nearly eight years and it’s been useful. Over time, it’s grown to be quite the flexible (and subsequently complex) set of APIs that we can use to create a PDF. You can review the iText JavaDocs here to learn how expansive the library is. It’s important to note that iText recently became a dual-license tool that moved from the LGPL license to an AGPL license. Per the license page, you must retain the producer line in each PDF that’s created. This makes for some unprofessional-looking PDFs unless you purchase the commercial version. I called iText to learn their commercial pricing, and while it isn't outlandish, it isn’t a drop in the bucket. Because of that, I instead opted to use the last LGPL version of iText, version 2.1.7.

The approach we’ll take is to utilize the existing QR4i open-source project, couple it with the iText 2.1.7 API set, and write a custom (yet variable) Java program that creates the wristband. Lastly, we’ll put an RPG wrapper over the Java program so we can easily invoke it from other RPG programs.

Writing the Java

Code Sample 1 shows the WristBand.java program that we’ll be invoking from RPG and passing it variable data to produce a wristband as shown in Figure 1. At the top of the program immediately following the "public class WristBand" declaration, we have the global variables defined that will be used to hold values, almost like how an RPG data structure holds values. The concept is that we’ll occupy all of these Java variables with values from our RPG, and once they’re occupied, we’ll invoke the Java createPDF() method. Each global parameter has what’s called a "getter and setter method" associated with it. For example, if we scroll to the bottom of Code Sample 1, we can see getBarCode() and setBarCode() Java methods. This is a common way Java allows you to occupy an object with data that can be later acted upon by another Java method—in this case, createPDF(). Moving on to Java method createPDF(), we see a new Document object is being instantiated by calling the Rectangle constructor, and then the Document constructor. The two parameters being passed to the Rectangle constructor are declaring the dimensions and then we rotate it 90 degrees to have it pointing the right direction as it hits the printer. The need to rotate would of course be relative to the media you were printing on.

Once the Document is created, we need to apply a “writer” to it, and that’s done with the PdfWriter.getInstance() static Java method. Notice we’re passing both the “doc” variable and the "filename" variable that stores the fully qualified path to where we want the file created in the IFS. Next, we start some “procedurally minded” code by opening the document with doc.open(). Once everything has been done with the document, it’s closed with doc.close(). In between the open and close, we add our PDF content. I make note of this because it resembles how we do green-bar creating—start with the headers, print the detail, close out with the summary and footers. Or you could relate it to the RPG cycle concerning the opening and closing of files, but we won't go there—wouldn’t want to get anybody’s old-school RPG heart beating too rapidly.

Aaron Bartell is Director of IBM i Innovation for Krengel Technology Inc. and an IBM Champion.



Advertisement

Advertisement

2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters