paint-brush
How to Generate a QR Code Image in Four Lines of Codeby@ssukhpinder
5,553 reads
5,553 reads

How to Generate a QR Code Image in Four Lines of Code

by Sukhpinder SinghJune 13th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A QR code is a computer-readable identification that contains data about the item to which it is attached. The article demonstrates how to return the QR Code image as a response from ASP.Net Core API. The “QRCoder” DLL helps generate QR codes with just four lines of code in C#. The author provides this code and software ‘AS IS’, without warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement.

Company Mentioned

Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - How to Generate a QR Code Image in Four Lines of Code
Sukhpinder Singh HackerNoon profile picture

A QR code is a computer-readable identification that contains data about the item to which it is attached. The article demonstrates how to return the QR Code image as a response from .Net Core API.

QR-Code Live Demo

Prerequisites

  • Visual Studio or Visual Studio Code already installed
  • Install the below package, i.e., “QRCoder” via NuGet package manager.

Let’s Start

The “QRCoder” DLL helps generate QR codes with just four lines of code in C#.

4 Lines of Code Snippet

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);

Description of each line of code

Initialize the QR code generator class: Create an instance of the QRCodeGenerator class.

QRCodeGenerator qrGenerator = new QRCodeGenerator();

Create QR code data: The next step is to initialize QR code data using the CreateQrCode method, which takes two arguments, i.e., string text for encoding inside the QR code, and another case defines the error correction level, i.e., ECCLevel.

Here, four different levels L (7%), M (15%), Q (25%), and H (30%) are available, whereby the percentage represents the hidden portion of the QR-code until the error correction algorithm can’t recreate the original message encoded in the QR code.

QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);

Generate QR code: The next step is to create the QR-code using the data initialized above.

QRCode qrCode = new QRCode(qrCodeData);

Create a graphical image: Finally, represent the QR code into a graphical image, as shown below. The

GetGraphic
method takes one argument, which defines the size of the QR-code. The
GetGraphic
method return image in the form of Bitmap by default.

Bitmap qrCodeImage = qrCode.GetGraphic(20);

How to return QR-code Image from .Net Core API response?

Controller GET route snippet uses the QR-code library and returns the QR code image as a response.

[HttpGet]
[Route("qenerate/{qrText}")]
public IActionResult GetQrCode(string qrText)
{
	QRCodeGenerator qrGenerator = new QRCodeGenerator();
	QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
	QRCode qrCode = new QRCode(qrCodeData);
	Bitmap qrCodeImage = qrCode.GetGraphic(20);
	return File(BitmapToBytes(qrCodeImage), "image/jpeg");
}

Bitmap to bytes function code snippet: QR-code, by default, generates a Bitmap, below function used to convert Bitmap to bytes.

private static Byte[] BitmapToBytes(Bitmap img)
{
	using (MemoryStream stream = new MemoryStream())
	{
		img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
		return stream.ToArray();
	}
}

Optional parameters & overloads: Example of QR-code with my publication “The Tech Masters” logo

//Set color by using Color-class types
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.DarkRed, Color.PaleGreen, true);
//Set color by using HTML hex color notation
Bitmap qrCodeImage = qrCode.GetGraphic(20, "#000ff0", "#0ff000");
//Set logo in center of QR-code
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)Bitmap.FromFile("C:\\myimage.png"));

Github Repo: consist of default & custom colored QR-code generation implemented in ASP.Net Core API.

ssukhpinder/QRCodeExample

Thank you for reading. Keep visiting and share this in your network.

Disclaimer: The author provides this code and software “AS IS”, without
warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement. In no event shall the
author be liable for any claim, damages or other liability in connection with the software or code provided here