Tips & Tricks - Creating QR Codes

QR codes are small images that usually represent a URL. You can navigate to the URL by scanning the QR code or using special apps. The ZXing Java library allows you to generate these QR codes yourself. This post shows you how to integrate this library and use it with Groovy to convert input into a QR code.

You can download the sample application here. You can then import them as usual.

The import file also contains a process that must be included in the import. The process is disabled after import. If you'd like to test this Tips & Tricks post, activate the process.

Enable the expert options so that all the dialog boxes mentioned in this example are accessible. Knowledge of Groovy scripting is an asset for this position.

Include Library

First, you must download the library from https://repo1.maven.org/maven2/com/google/zxing/core/3.1.0/core-3.1.0.jar and make it available to Intrexx. Since the file name isn't very descriptive, the file is renamed to "zxing-3.1.0.jar." Copy the file to the "lib/extensions" directory in the portal.

Now the portal needs to be told where to look for this library. To do this, edit the "portal.wcf" file, which you'll find in the portal directory internal/cfg. In the following section, add the bottom line:

            # Java Classpath (include wrapper.jar)  Add class path elements as
#  needed starting from 1
wrapper.java.classpath.1=<Intrexx-installation-directory>\lib\update
wrapper.java.classpath.2=<Intrexx-installation-directory>\lib\*.jar
wrapper.java.classpath.3=<Intrexx-installation-directory>\lib\remote\*.jar
wrapper.java.classpath.4=<Intrexx-installation-directory>\derby\lib\*.jar
wrapper.java.classpath.5=<Intrexx-installation-directory>\lib\extensions\*.jar

        

It is important here to increment the value in `wrapper.java.classpath.*` as a sequential number. Then restart the portal service.

Application

Here you can see the input page of the sample application, where you can enter and save the URL. The data group contains the "Image" file data field.

On the "Overview" page, you'll find a table that displays the data from the data group. When you edit the "Image" column, you'll see that a template for displaying the QR code in the table is selected here.

Process

In the associated process, there is a data group event handler that responds to the insertion of records into the example application's data group.

The event handler is associated with a Groovy action that uses the following Groovy script:

            import java.awt.Color
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.File
import java.util.Hashtable

import javax.imageio.ImageIO
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.common.BitMatrix 
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel

//URL to be converted
String recURL = g_record["5BD2494E2A4BD0AD0C427262CE4882C61F639EF3"].value /* datafield URL <string> */ 

//Size of the image in pixels 
int size = 125 

//Set error correction level 
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>() 
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L) 

//Initialize QR Code Writer 
QRCodeWriter qrCodeWriter = new QRCodeWriter() 

//Calculate QR code from URL 
BitMatrix byteMatrix = qrCodeWriter.encode(recURL,BarcodeFormat.QR_CODE, size, size, hintMap) 

//Adapt length unit of the image to QR code 
int CrunchifyWidth = byteMatrix.getWidth() 

BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth,BufferedImage.TYPE_INT_RGB)
image.createGraphics()
Graphics2D graphics = (Graphics2D) image.getGraphics() 

//Color the entire image white
graphics.setColor(Color.WHITE)
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth) 

//Change color to black
graphics.setColor(Color.BLACK)

//Read black points and set to image 
for (int i = 0; i < CrunchifyWidth; i++) 
{
    for (int j = 0; j < CrunchifyWidth; j++) 
	{
        if (byteMatrix.get(i, j)) {
            graphics.fillRect(i, j, 1, 1)
        }
    }
}

// Convert image to image file
String fileType = "png"
String filePath = "internal/tmp/qrcode-${g_record.getRecId()}.png"
File qrFile = new File(filePath)
ImageIO.write(image, fileType, qrFile)

//Move image to data group
g_dgFile.move(guid: "F80E0F688327BCF0D35A91E5B4930E731861CCA4", id: g_record.getRecId(), file: qrFile, deleteAlways: true, triggerWorkflow: false)
        

If you want to use the script in other applications, please replace the GUID "5BD2494E2A4BD0AD0C427262CE4882C61F639EF3" with the GUID of your URL data field and the GUID "F80E0F688327BCF0D35A91E5B4930E731861CCA4" with the GUID of your image file data field.

Browser-based application

When you open the application in your browser, enter a URL on the input page, and save it, a QR code is automatically generated and displayed on the "Overview" page. From there, the image can be easily copied to the clipboard, for example.