QR Code Generation and Verification
In this
blog post you will learn all about QR Code, starting from requirement to
generation to verification. I am going to develop this in C# and in ASP.NET Web
Forms, but you can use it with MVC, Desktop Apps etc. I will be using MessagingToolkit.QRCode open source library
for everything here.
Requirement
Let’s
assume we have a requirement to print QR Code on certificates. The purpose of
this QR Code on certificate is to validate/verify it anytime by anybody. You
can restrict the verification if you want but I’m going to develop so that
anyone can scan QR Code printed on the certificate and check details of the
certificate. This way certificate fraud print will not be possible and easily
be recognized.
If this
make sense, let’s develop this.
Generating
QR Code
Generating
this is pretty simple, let’s follow the steps to generate QR Codes.
Step 1: Create a project, I’ll create simple
ASP.NET Web Forms project.
Step 3: Define hosting URI and randomly
generate QR Code file name, as give below:
string domain = "http://localhost:2779/";
string
qrCodeImgFileName = new Random().Next().ToString() + ".jpg";
Step 4: Generate message to be printed on QR
Code, I’ll be printing QR Code file URI itself on the QR Code, you can
customize/update according to your need later on.
string qrCodeInformation = domain + "ValidateQR.aspx?identity=" + qrCodeImgFileName;
QRCodeEncoder qe = new QRCodeEncoder();
System.Drawing.Bitmap bm =
qe.Encode(qrCodeInformation);
Step 5: Once you have QR Code bits as Bitmap,
go ahead and save QR Code bitmap into memory stream and save memory stream into
file.
//
Save QR Code bitmap to memory stream
var memStream = new MemoryStream();
bm.Save(memStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
//
Save Jpeg stream into file
using (var fileStream = new FileStream(Server.MapPath("~/QRCodes/") + qrCodeImgFileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
memStream.Position = 0;
memStream.CopyTo(fileStream);
}
Step 6: If you run your project here, you
will see a file gets created inside QRCodes folder on the root. But let’s take
this one step ahead and display this on the same web page just after QR Code
generation.
Image1.ImageUrl
= "~/QRCodes/" + qrCodeImgFileName;
Label1.Text = qrCodeInformation;
Here is
the output of the generated QR Code and the URI you see below in the image is
the information printed on the QR Code. So, if you scan this image right now,
you will see that URI.
That’s
all you need to generate QR Code. Now, let’s implement its verification system.
Verification
of QR Code
In
order to verify the QR Code, one will scan this from device, that device will
decode the QR and display that message. In my case that message is URI in the
image above.
If you
scan above QR Code using https://webqr.com/,
you will see following option to navigate. Notice the URI is exact same here as
well.
Let’s
recall our context here again, student is provided a certificate with QR Code
and one will scan that and navigate to verification website.
Now, either
we need a logic to read the URI and provide full details of that certificate
from our certificate database. Or, we feed up the QR Code file to MessagingToolkit.QRCode and this will decode
information, in our case that information is nothing but complete URI.
For the
sake of this demo, I’ll just test if that QR Code file is exist or not. Here’s
the code, which will execute on page load.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["identity"] != null)
{
string qrCodeFileName = Server.MapPath("~/QRCodes/") +
Request.QueryString["identity"];
// Check
QR Code file available
if(File.Exists(qrCodeFileName))
{
imgQRCode.ImageUrl = "~/QRCodes/" +
Request.QueryString["identity"];
lblMessage.Text = "QR Code is correct and found.";
// TODO:
Your database calls
}
else
{
lblMessage.Text = "QR Code not found or invalid.";
}
}
else
{
Response.Redirect("~/GenerateQR.aspx");
}
}
Notice,
I’m reading query string identity and if its available,
generating QR Code full file URI and then checking its existence in file
system. And if file is available, QR Code will be displayed again with a
message QR Code is correct and found. If QR Code file is
not available, this will display a message QR
Code not found or invalid.
Hope
this helps.
Hello Abhimanyu Ji, I'm Sanjeev from Allahabad, UP India. I found most of helpfull of your site's Videos, Soluction and All of .I want to meet you, Please Contect me. +91 9621027790. Thank You.
ReplyDelete