In this article I will explain with an example, how to read
QRCode Image in ASP.Net Core (.Net Core 2) MVC.
Installing System.Drawing.Common library
First, you will need to install the System.Drawing.Common package.
Install ZXing.Net.Bindings.Windows.Compatibility package using Nuget
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Drawing;
using ZXing;
using ZXing.Common;
using ZXing.Windows.Compatibility;
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method gets called, when a File is selected and the Read Button is clicked.
Inside this Action method, the selected file is received in the IFormFile as parameter.
Then, an object of MemoryStream class is created and the selected file is copied into it and a BitMap class object is created and object of MemoryStream is saved in it.
After that, Bitmap is saved in the BitmapLuminanceSource object and then, the source is converted to BinaryBitmap object.
Finally, the QR Code is read using
decode method of
MultiFormatReader class and the decoded value is set into
ViewBag object.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(IFormFile postedFile)
{
using (MemoryStream ms = new MemoryStream())
{
//Create Bitmap from selected file.
postedFile.CopyTo(ms);
using (Bitmap bitMap = new Bitmap(ms))
{
//Read theQRCode Image.
BitmapLuminanceSource source = new BitmapLuminanceSource(bitMap);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
// Reader object accept BinaryBitmap and decode it.
Result result = new MultiFormatReader().decode(binaryBitmap);
ViewBag.QRText = result.Text;
}
}
return View();
}
}
View
HTML Markup
The View consists of an HTML Form created with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form element has been specified with enctype attribute and its value must be multipart/form-data, otherwise the IFormFile parameter will be NULL.
Inside the HTML Form there is an HTML FileUpload, a Submit Button and a SPAN element.
Submitting the Form
When the
Submit Button is clicked, the
ViewBag object is checked for NULL and if it is not NULL then the value of the
ViewBag is displayed using SPAN element.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
<input type="file" name="postedFile" />
<input type="submit" id="btnRead" value="Read" />
<br /><br />
@if (ViewBag.QRText != null)
{
<span>@ViewBag.QRText</span>
}
</form>
</body>
</html>
Screenshot
Downloads