You can generate QR-code and EAN-13 barcode images in a MDriven Turnkey application by declaring the supported late-bound static methods in your model and calling them from OCL; this page is for modelers who want to display or distribute those images from a ViewModel.
What you create
The QR-code pattern uses a model class named ZXing. MDriven provides the implementation behind this pattern. Your model declares the method so that OCL can call it; you do not implement QR-code encoding logic yourself.
The result is an Image. Store it in a derived attribute of type Blob. The QR code can then be treated as an image, including in grids and reports.
Limit: Do not use this pattern in a server-side job.
Generate a QR code
Configure the ZXing class
- In your model, add a class named
ZXing. - Add an operation named
QRImagewith this signature:
QRImage(width: Integer; height: Integer; value: String): Image
- Set the operation kind to
static. - Mark the operation as
IsQuery. This declares that the operation does not change model-object state and permits its use in OCL. - Add the tagged value
Eco.ExternalLateBound = Trueto the operation.
| Setting | Value | Why it is required |
|---|---|---|
| Class | ZXing
|
Identifies the supported QR-code and barcode pattern. |
| Operation | QRImage(width: Integer; height: Integer; value: String): Image
|
Accepts image dimensions and the text to encode, and returns an image. |
| Operation kind | static
|
Lets OCL call the operation on the class: ZXing.QRImage(...).
|
| IsQuery | Enabled | Allows the operation to be used in OCL. |
| Tagged value | Eco.ExternalLateBound = True
|
Connects the declared model operation to the provided implementation. |
Add a derived image attribute
Add a derived attribute of type Blob to the class for which you need a code. Set its derivation OCL to call ZXing.QRImage.
For example, a return item with a Lopnummer attribute can derive a 300 by 300 pixel QR code that opens a Turnkey view and passes the return number in a view variable:
QRCode: Blob
DerivationOcl = ZXing.QRImage(300,300,'https://reklamation.azurewebsites.net/MDriven/DisplayWithVariables?view=TestQRCodeFromVariable&id=$null$&vLopnummer='+self.Lopnummer.asstring)
In this example:
300,300requests a 300 by 300 pixel image.- The third argument is the string encoded in the QR code.
self.Lopnummer.asstringappends the current object's return number to that string.- The derived
QRCodeBlob is an image and can be shown where your ViewModel presents image Blob values.
Present the image
Expose the derived Blob attribute in the ViewModel that displays the object. You can then present the QR code in a grid or report, email it, print it, or show it with the object details.
Make the image large enough for the intended scanner. Turnkey provides small, medium, and large image style references, which can be changed through CSS.
Generate an EAN-13 barcode
EAN-13 is a numeric barcode format. To generate one, configure a second static late-bound operation on the same ZXing class.
- Add an operation named
BarcodeImagewith the same parameters and return type asQRImage:
BarcodeImage(width: Integer; height: Integer; value: String): Image
- Set its operation kind to
static. - Mark it as
IsQuery. - Add the tagged value
Eco.ExternalLateBound = True. - Call
ZXing.BarcodeImagefrom the derivation OCL of a derivedBlobattribute, using the same pattern as the QR-code example.
The value must be either:
- A 12-digit string containing only digits
0through9. The barcode generator adds the thirteenth checksum digit. - A 13-digit string containing only digits
0through9, where the final digit is the correct checksum digit.
For example, if self.ArticleNumber contains a valid 12-digit EAN-13 base number, derive a barcode image with:
EAN13Barcode: Blob
DerivationOcl = ZXing.BarcodeImage(300,300,self.ArticleNumber.asstring)
Use QR codes to open a Turnkey workflow
A QR code can encode a URL that opens a specific view and supplies values to that view. The earlier example uses the DisplayWithVariables verb with view=TestQRCodeFromVariable, id=$null$, and vLopnummer. When a phone scans the code, the target view receives the encoded Lopnummer value in its vLopnummer variable.
Design the target view and its access deliberately. A view left out of AccessGroups can be opened by anyone who has the URL. Do not treat an easily guessed business number as authorization. If possession of the URL is intended to grant access, use a value that is difficult to guess, such as a generated Guid, and limit the actions available in the target view.
For the complete workflow pattern, including reacting once when a supplied view variable changes, see Documentation:QR-Code to drive a workflow in any MDriven turnkey app.
Troubleshooting
| Symptom | Check |
|---|---|
| The OCL call is not accepted | Confirm that the operation is static and marked IsQuery.
|
| The model operation does not resolve to the supplied implementation | Confirm the class name is ZXing, the operation name and signature match, and the operation has Eco.ExternalLateBound = True.
|
| The image does not scan reliably | Increase the requested dimensions and use an appropriate image style reference for the display location. |
| An EAN-13 barcode cannot be generated | Ensure the value contains only digits and is either 12 digits, or 13 digits with a correct final checksum digit. |
| Generation is attempted from a server-side job | Move the generation to the Turnkey/ViewModel use case. This pattern does not work in a server-side job. |
