QR Integration: Create Your Own QR API Service with MuleSoft

Shubham Chaurasia
4 min readNov 6, 2022

--

❒ Introduction About QR:-

•QR Stands for Quick Response Code

•QR Code® is a two-dimensional barcode created by the Japanese corporation Denso-Wave by Masahiro Hara in 1994

• It can store a maximum of 7089 digits or 4,296 alphanumeric characters including punctuation marks and special characters. They can also store binary/bytes and kanji(kanji are Japanese symbols representing entire words)

  • Note: A QR Code can’t hold enough information to encode a complete image

❒ Meetup Session/ Youtube Recording

I have delivered a meetup on this topic. You can watch it on Youtube below

👉QR Integration — Creating a QR Service with MuleSoft

❒ MuleSoft Integration with QR Code

There are two ways to Integrate QR.

  • Using a third-party QR API Service
  • Using a Java Library to create your own QR Code generating API

First, we will look at using a third-party QR API Service. For this, we will be using goqr.me API.

  1. Create a new project, Drag HTTP Listener, and configure your path (/api/qrcode)

2. Next Drop an HTTP Request Connector from Palette in the flow, and configure it as per below configuration

Set the HTTP Request Configuration like below

Protocol:- HTTP/HTPS
Host:- api.qrserver.com
BasePath:- /v1
Method:-
Path:-/create-qr-code

3. In HTTP Request Configuration, Go to Settings Tab, Add Query Parameters inline, to receive the data and size from the user.

4. Run the Project and hit the configured endpoint, also providing the query Parameters data and size. You will receive the QR image.

generating qr image from 3rd party api

Now, we will be using a Java Library to create your own QR Code API Service.

  1. Add the following dependency to your POM File. This will add the QRGen Library to your project.

<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.4</version>
</dependency>

2. Add an HTTP Listener and Configure it as below. Path(/java/qrcode)

3. Add a Transform message component, and add the below code.

%dw 2.0
output application/json
var sizeArr=attributes.queryParams.size splitBy('x') default "100x100"
---
{
path:p('mule.home') ++ '/bin/',
row:sizeArr[0],
col:sizeArr[1],
data:attributes.queryParams.data
}

4. Create a Java Class File(qr.java) with the below code under src/main/java.

import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class qr {
public static String qrfunc(String data,int r1, int c1) throws IOException{
File file = QRCode.from(data).to(ImageType.PNG)
.withSize(r1, c1)
.file();
String fileName = “qrCode” +”.png”;
Path path = Paths.get(fileName);
if ( Files.exists(path)){
Files.delete(path);
}
Files.copy(file.toPath(), path);
return path.toAbsolutePath().toString();
}
}

5. Drop Invoke Static Connector from palette in the flow and configure it like the below image.

6. Add Read File Connector from Palette in the flow. Go to the MIME Type Tab, and Select MIME type as image/PNG from the drop-down menu.

7. Configure the Read Connector Configuration as shown below.

8. Set File Path as “qrCode” ++ “.PNG”

In the end, your flow will look like below image.

9. Run the Project and hit the configured endpoint, also providing the query Parameters data and size. You will receive the QR image.

Note:- This is a WiFi QR, when scanned, you can directly connect to the network using Google Lens or Camera or QR Reader App. No need to enter Wifi Password etc. Check the below customization to generate your own.

❒ Customizations:-

You can create not only Text/Links QRs but WIFI, VCards, PDFs also.

To Create a WIFI QR, Enter the Data in the below format

WIFI:S:<SSID>;T:<WEP|WPA|blank>;P:<PASSWORD>;H:<true|false|blank>;

Ex:- WIFI:S:MySSID;T:WPA;P:MyPassW0rd;;

Thank You for sticking with the article until the end. If you found this helpful, please leave a Clap & Follow for more such amazing articles in the future.

Also Checkout More Amazing Articles:- Click Here

Feel Free to Drop your queries on my below Communication Channels

Gmail:-myid535@gmail.com
LinkedIn:-
https://www.linkedin.com/in/shubhamchaurasia1/

--

--

Shubham Chaurasia

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/