QR Code Tutorial

Table of Contents

Implement a QR code scanner and generator


This tutorial will demonstrate the use of the <mobi:scan> and <mobi:qrcode> components for reading and creating QR codes. The ICEmobile container is required as the <mobi:scan> component will access the device's camera and try and read the QR code. If successful the decoded qr code will be assigned to the backing bean. The <mobi:qrcode> component can be used to generate a QR code image (implemented via the zxing library). The component takes a string value and will return the corresponding QR code graphic. Additional information regarding these components can be found here:

The full source for this sample can be found here.

Usage


The <mobi:scan> tag is very easy to use. Our first step is to add the component, a <mobi:commandButton> to submit the value, and a <mobi:fieldsetGroup> for the layout. The <mobi:scan> component will leverage the device's camera and allow us to capture the QR code which we can display by clicking the "Display Scanned Code" button. Please paste the following into an ICEmobile page:

qrscan.xhtml
   <mobi:fieldsetGroup>
       <mobi:fieldsetRow>
	  <h:outputLabel value="Scan a code:" />
	  <mobi:scan value="#{qrCodeBean.qrScanCode}" buttonLabel="Scan a code" 
             captureMessageLabel="Code Captured! Scan again?" />
        </mobi:fieldsetRow>
   </mobi:fieldsetGroup>
   <mobi:fieldsetRow>
          <mobi:commandButton value="Display Scanned Code: " />
	  <h:outputText value="#{qrCodeBean.qrScanCode}" />
   </mobi:fieldsetRow>


The next step is to generate a custom QR code using a user entered value. We will add the <mobi:qrcode> to display the code and a commandButton to submit the value of the <mobi:inputText>. Paste the following code below the <mobi:scan> fieldsetRow:


qrscan.xhtml
   <mobi:fieldsetRow>
	<h:outputLabel value="Enter QR code:" />
	<mobi:inputText type="text" value="#{qrCodeBean.qrGenCode}" />
   </mobi:fieldsetRow>
   <mobi:fieldsetRow>
	<mobi:commandButton value="Create QR Code"/>
   </mobi:fieldsetRow>
  <mobi:fieldsetRow>
        <h:panelGroup>
	   <h:outputLabel value="Scan Me:" />
	   <br />
	   <mobi:qrcode value="#{qrCodeBean.qrGenCode}" />
	 </h:panelGroup>
  </mobi:fieldsetRow>


QrCodeBean.java


Create a bean named QrCodeBean.java. The generated QR code qrGenCode, will have a default value of the ICEsoft website while the qrScanCode field will be populated with the value of the scanned QR code. Please add the following markup into a managed bean:

QrCodeBean.java
@ManagedBean
@ViewScoped
public class QrCodeBean implements Serializable {

    private String qrScanCode;
    private String qrGenCode = "http://www.icesoft.com";

    //TODO GENERATE GETTERS/SETTERS


Run the application


Open the application in a separate browser to display a QR code. Using a mobile device with the ICEmobile SX container installed, scan the QR image and select "Display Scanned Code". We can then modify the value of the QR code by entering in our own code and clicking "Create Code".



Source

1. qr-scan-basic.zip

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

© Copyright 2016 ICEsoft Technologies Canada Corp.