Step-by-Step Guide to QR Code Generation in MuleSoft | MuleSoft Guide

Author: Keerthi Dasari

LinkedIn: Keerthi Dasari


  1. QR Code Overview: A QR code (short for “quick response code”) is a type of matrix barcode.
  2. Mulesoft and QR Code Generation: Currently, there is no built-in Mule component that can generate QR codes from provided strings or URIs in Mule 4. However, we can create our own solution using a third-party Java library called “ZXing.”
  3. Solution Approach: Our approach involves creating a custom Mule API endpoint. When a user calls this endpoint with a query parameter (a string), it triggers a Java function capable of generating QR codes. The function then returns an encoded string representing the QR code, which Mulesoft can convert to a PNG format.
  4. Implementation Steps:
  • Step 1: Java Code (“GenerateQrCode.java”): We’ve used the ZXing Java library in our example. This code snippet demonstrates how to create a QR code using ZXing. It converts the input data (string) into a QR code image.
package com.utilsimportimport java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Base64;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;public class GenerateQrCode {//static function that creates QR Codepublic static String generateQRcode(String data, int heightOfQR, int widthOfQR) throws WriterException, IOException{String charset = "UTF-8";//the BitMatrix class represents the 2D matrix of bits//MultiFormatWriter is a factory class that finds the appropriate Writer subclass for the BarcodeFormat requested and encodes the barcode with the supplied contents.BitMatrix matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, widthOfQR, heightOfQR);//System.out.print( MatrixToImageWriter.toBufferedImage(matrix));//Converting BitMatrix to PNGString imageString = null;ByteArrayOutputStream bos = new ByteArrayOutputStream();try {ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", bos);byte[] imageBytes = bos.toByteArray();//Convert PNG to Base64 StringBase64.Encoder encoder = Base64.getEncoder();imageString = encoder.encodeToString(imageBytes);bos.close();} catch (IOException e) { e.printStackTrace(); }//Return Base64 String to Calling Functionreturn imageString;}};

  • Step 2: Integration with Mulesoft Studio: Paste the Java code into the src/main/java directory of your Mulesoft Studio project, within the specified package.
  • Step 3: Add Dependencies: Update your Mule’s POM file to include the necessary ZXing dependencies:
<dependency>               <groupId>com.google.zxing</groupId>               <artifactId>core</artifactId>               <version>3.3.0</version></dependency><dependency>               <groupId>com.google.zxing</groupId>               <artifactId>javase</artifactId>               <version>3.3.0</version></dependency>

Step 4: Mule Flow structure:

  1. Minimize Image
  2. Edit Image
  3. Delete Image
Mule Flow structure
Mule Flow structure

Step 5:

  1. Add a Transform Message Component.
  2. Insert the following code into the component:
%dw 2.0output application/java---// Extract the value of the "data" query parameter from theattributes.queryParams.data as String default "Empty"

Step 6: Insert a Transform Message component into your workflow. This component will be used to manipulate the data as needed. You can specify the name for the QR code generation according to your requirements, or alternatively, parameters can be passed directly. Below is the sample code to be added to the Transform Message component:

%dw 2.0// Import the GenerateQrCode class from the specified Java packageimport java!com::utils::GenerateQrCode// Import all the functions from the Binaries module in DataWeaveimport * from dw::core::Binaries// Set the output format as a PNG image with binary encodingoutput image/png with binary// Store the input payload in a variable named 'data'var data = payload// Define the height of the QR code image in pixelsvar heightOfQR = 300// Define the width of the QR code image in pixelsvar widthOfQR = 300---// Use the imported Java function to generate a QR code from the 'data' variable// The QR code is generated with the specified height and width// The result is then converted from a Base64 string to a binary formatfromBase64(GenerateQrCode::generateQRcode(data,heightOfQR,widthOfQR)) as Binary
  1. The provided code specifies the size of the QR Scanner.
  2. Proceed to run the project and access the ‘HTTP Listener’ endpoint.
  3. Below, you’ll find the QR code generated using this API. Feel free to scan it.

Step 1: Set the QR Scanner size in the provided code.

Step 2: Run the project and access the ‘HTTP Listener’ endpoint.

Step 3: Scan the QR code generated via this API.

A QR code displayed in a web interface, showing a black and white matrix barcode with associated HTTP request information.
]]>
Post a Comment (0)
Previous Post Next Post