arrow-down

QR Code Library

Contents

Library for generating QR Code images that can be used from JavaScript controllers in XP apps.

qrcode

To start using this library, add the following into your build.gradle file:

dependencies {
    include "com.enonic.lib:lib-qrcode:2.0.0"
}

repositories {
    maven { url "https://jitpack.io" }
}

Usage

To use this library in your JavaScript code, it first needs to be required:

var qrcode = require('/lib/qrcode');

To generate an image with a QR Code just call the generateQrCode function with the desired parameters.

var qrcode = require('/lib/qrcode');

exports.get = function (req) {
    var image = qrcode.generateQrCode({ (1)
        text: 'https://enonic.com',     (2)
        size: 250                       (3)
    });

    return {
        body: image,                    (4)
        contentType: 'image/png'        (5)
    }
};
1 Generate the QR Code. The result will be a stream object with the generated PNG image.
2 Pass the text or URL to be encoded.
3 Optionally specify the size of the square image, in pixels.
4 Return the image as the body of the HTTP response.
5 Set the content type of the response to PNG.

API

The following function is defined in this library.

generateQrCode(options)

Generates a PNG image with a QR Code that encodes the specified text. The value returned is a stream object. It can be set as the response of an HTTP request, stored in a node or content, or manipulated using the lib-io.

Parameters

The request function takes a parameter object with options. The text option is required.

  • options (object) Parameters to generate the QR Code.

    • text (string) The text or URL to be encoded in the QR code.

    • size (number) The width and height of the square image generated. The default value is 250.

Returns

The function will return a stream binary object containing a PNG image.

Compatibility

This library requires Enonic XP release 6.11.0 or higher.


Contents

Contents