Tips & Tricks - Generate QR codes

QR codes are small pictures that usually represent a URL. When the QR code is photographed or viewed in specialized apps, the user can navigate to the URL. The Java library ZXing provides you with the ability to create QR codes yourself. This Tips & Tricks article will demonstrate how to incorporate this library and use it with Groovy to transform entries into a QR code. The example application for this workshop can be downloaded here. It can be imported into your portal as usual. Make sure the expert options have been activated so that all of the dialogs explained in this example are available. Background knowledge in Groovy are an advantage in this workshop.

Incorporate the library

First of all, the library needs to be downloaded and made available to Intrexx. The .jar file can be downloaded from: https://repo1.maven.org/maven2/com/google/zxing/core/3.1.0/core-3.1.0.jar. Because this filename is not particularly meaningful, the name should be changed to "zxing-3.1.0.jar". In principle, Java libraries can either be made available to a single portal or to all of them. To use the library in every portal, the library needs to be copied into the Intrexx installation directory \lib\extensions\. Now the portal needs to be informed where it should search for this library. To do this, the file "portal.wcf" in the portal directory internal/cfg needs to be edited and a line with the path to the .jar file needs to be added at the bottom of this section: It is important here that the value after wrapper.java.classpath.* increases incrementally. Add the last line at the bottom of this section:

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

It is important here that the value after wrapper.java.classpath.* increases incrementally. This has to be a consecutive number. Finally, the portal service needs to be restarted.

Application

Here is the edit page of the example application where the URL can be entered and saved. The data group contains the file data field "Image".

The "All entries" page contains a view tables that displays data from the data group. If you edit the "Image" column, you will see that the second template from the left has been selected. With this selection, the created QR code will be shown in the table later.

Process

The included process contains a data group event handler that responds to data records being added to the data group in the example application.

A Groovy script action is connected to the event handler with the following Groovy script:

import de.uplanet.lucy.server.businesslogic.util.FileUCHelper

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

//Zu konvertierende URL
String recURL = g_record["5BD2494E2A4BD0AD0C427262CE4882C61F639EF3"].value /* datafield URL <string> */ 

//Größe des Bildes in Pixel 
int size = 125 

//Fehlerkorrekturlevel setzen 
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>() 
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L) 

//QR-Code Writer initialisieren 
QRCodeWriter qrCodeWriter = new QRCodeWriter() 

//QR-Code aus URL berechnen 
BitMatrix byteMatrix = qrCodeWriter.encode(recURL,BarcodeFormat.QR_CODE, size, size, hintMap) 

//Längeneinheit des Bildes an QR-Code anpassen 
int CrunchifyWidth = byteMatrix.getWidth() 

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

//Komplettes Bild weiss einfärben
graphics.setColor(Color.WHITE)
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth) 

//Farbe auf Schwarz wechseln
graphics.setColor(Color.BLACK)

//Schwarze Punkte auslesen und auf Bild setzen 
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)
        }
    }
}

// Image in Bild-Datei umwandeln
String fileType = "png"
String filePath = "internal/tmp/qrcode-${g_record.getRecId()}.png"
File qrFile = new File(filePath)
ImageIO.write(image, fileType, qrFile)

//Bild in Datengruppe verschieben
FileUCHelper.moveFileToIntrexx(g_context, qrFile, "F80E0F688327BCF0D35A91E5B4930E731861CCA4", g_record.getRecId(), false)

If you use the script in other applications, 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.

Application in the browser

If you open the application in the browser, enter the URL on the edit page and save it, a QR code is generated automatically and displayed on the "All entries" page. This image can then be copied to the clipboard from here, for example.