Show Navigation

qrcode

A plugin that allows you to create QR codes as part of your Grails application without the need for an external service

Owner: technipelago | 0.9 | Mar 31, 2016 | Package | Issues | Source | License: Apache-2.0


dependencies {
    compile 'org.grails.plugins:qrcode:0.9'
}

            

This plugin allows you to create QR codes as part of your Grails application without the need for an external service.

Installation

For Grails 2 use version 0.7, for Grails 3 use version 0.8+.

Grails 2

Add a dependency in BuildConfig.groovy:

grails.project.dependency.resolution = {
  // ...
  plugins {
    compile ':qrcode:0.7'
    // ...
  }
}

Grails 3

Add a dependency in build.gradle

compile 'org.grails.plugins:qrcode:0.9'

Usage

QrcodeController

Render text QRCode at default size (300x300px)

.../qrcode/text/Hello+World

Render text QRCode in 30x30px

.../qrcode/text?w=30&text=test

QRCode

Render url QRCode

.../qrcode/url?u=http://grails.org/plugin/qrcode

Configuration

The maximum value of the width parameter can be configured with qrcode.size.max (default 1024).

qrcode.size.max = 2048

QrCodeService

qrCodeService.renderPng("test", 30, outputStream)

Tag Library

Namespace: qrcode

Render text as QRCode

<qrcode:image height="100" width="100" text="TEST TEXT"/>

If you want to include a QRCode image in an email and you use a GSP to render email content you must set attribute absolute="true". Otherwise the image url will not start with http:// and will probably not render correct.

<qrcode:image height="100" width="100" text="#648357" alt="Invoice #648357" absolute="true"/>

Render current request url as QRCode

<qrcode:url width="64"/>

Render vCard contact information as QRCode

In this example we have a method on the Person domain class that returns contact information as a vCard formatted String.

class Person {
    ...
    transient String getVcard() {
        def s = new StringBuilder()
        s << "BEGIN:VCARD\n"
        s << "VERSION:3.0\n"
        s << "N:${lastName};${firstName};;;\n"
        s << "FN: ${fullName}\n"
        s << "ORG:${companyName}\n"
        s << "TITLE:${title ? title.replace(',', '\\,') : ''}\n"
        s << "TEL;TYPE=work,voice,pref:${phone}\n"
        s << "TEL;TYPE=cell,voice:${cellphone}\n"
        s << "EMAIL;type=internet,pref:${email}\n"
        s << "ADR;TYPE=work,postal,pref:;;${address};${city};${state};${postalCode};${country}\n"
        def timestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
        s << "REV:${timestampFormat.format(lastUpdated ?: dateCreated)}\n"
        s << "END:VCARD\n"
        return s.toString()
    }
}

Now it's easy to render a QRCode of the contact information. This QRCode can be scanned with a smartphone and imported as a contact.

<qrcode:image height="150" text="${person.vcard}" alt="${person.fullName} ${person.address} ${person.city}"/>

Changes

Version 0.9: Prevent DoS attempts using large size/width values.

Version 0.8: Grails 3 support.

Version 0.7: Upgraded zxing dependency to 3.2.0.

Version 0.6: Upgraded pngj dependency to 2.1.0.