Quick to Read QR Code in C#
You can quickly scan qrcodes from an image file in your C# program using C# barcode reader library.
- Call method
BarcodeScanner.Scan()with QR Code image file path, and scanned barcode formatBarcodeType.QRCode, you will get all QR Codes data messages inside the scanned image file - Call method
BarcodeScanner.ScanInDetails(), you will get all detected QR Codes with detailed information, such as each QR Code location in the image, QR Code detailed configuration, such as version, encoding data mode. View details here How to scan, read QR Code location information using C#?
string[] datas = BarcodeScanner.Scan("qrcode-sample.png", BarcodeType.QRCode); BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails( "qrcode-sample.png", BarcodeType.QRCode);
Scan QR Codes from image objects
The following C# source codes show how to scan and read QR Codes from image data in byte array or
Stream object.
- Both method
ScanandScanInDetailsacceptStreamobject as source QR Code image data
byte[] dataBytes = File.ReadAllBytes("sample-barcode.png"); Stream barcodeInStream = new MemoryStream(dataBytes); string[] datas = BarcodeScanner.Scan(barcodeInStream, BarcodeType.QRCode); BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails( barcodeInStream, BarcodeType.QRCode);
Scan QR Code with other barcode formats from a single image file
If your barcode image contains QR Codes and other barcode formats, you can use the following demo code to scan and detect all of them from the image file in one barcode scanning.
- Pass the list of the target barcode type (for example,
BarcodeType.QRCodeandBarcodeType.Code128) to the methodScanorScanInDetails
BarcodeDetail[] datas = BarcodeScanner.ScanInDetails("multiple-barcodes.png", new List<BarcodeType> { BarcodeType.QRCode, BarcodeType.Code128 });
Scan and read all QR Code informations from an image file using C#
Call method
Here are some information about BarcodeDetail
BarcodeScanner.ScanInDetails() with the same input parameters, you will get all scanned QR Codes with detailed information in BarcodeDetail objects,
such as scanned QR Code region.
BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails( "qrcode-sample.png", BarcodeType.QRCode);
Here are some information about BarcodeDetail
- Rotation: get the scanned QR Code rotation angle
- X1, Y1, X2, Y2, X3, Y3, X4, Y4: 4 coordinate locations on the scanned image to cover the scanned QR Code symbol
- IsGS1Compitable: If true, the scanned QR Code is a GS1 compatible QR Code. You need read the GS1 data message from it.
- IsStructuredAppend: If true, the scanned QR Code symbol is part of the Structured Append QR Code symbols
- GetDataBytes(): Get the QR Code data in byte array.
- GetMessage(): Get the QR Code data in specified encoding format.
Extract QR Code Barcode Properties
In scanned QR Code
BarcodeDetail object, call method GetBarcodeProps() to retrieve QR Code barcode property information.
- Version. QR Code version used in encoding.
- DataMode. QR Code encoding data mode. If mixed, it will return
QRCodeDataMode.Mix - ECL. QR Code error correction level.
- IsStructuredAppend. Whether QR Code is in structured append mode.
- SymbolPosition. If it is in Structured Append mode, get it's symbol position. Start from 1.
- SymbolCount. If it is in Structured Append mode, get number of symbols in the group.
- Parity. If it is in Structured Append mode, get 8-bit parity data.
ScanOptions ops = new ScanOptions(BarcodeType.QRCode); BarcodeDetail[] result = BarcodeScanner.Scan(imgFilePath, ops); if (result != null && result.Length > 0) { foreach (BarcodeDetail b in result) { Console.WriteLine("Message: {0}", b.GetMessage()); if (b.Type == BarcodeType.QRCode) { QRCodeProps props = (QRCodeProps)b.GetBarcodeProps(); // Get version used in encoding. Console.WriteLine("Version: {0}", props.Version.ToString()); // Get data mode used in encoding. // QRCodeDataMode.Mix for more than 1 data modes used. Console.WriteLine("Data Mode: {0}", props.DataMode.ToString()); // Get error correction level used in encoding. Console.WriteLine("ECL: {0}", props.ECL.ToString()); // Indicate if it contains structure append block. Console.WriteLine("Is Structure Append: {0}", props.IsStructuredAppend); if (props.IsStructuredAppend) { // Get symbol position. Start from 1. Console.WriteLine("Symbol Position: {0}", props.SymbolPosition); // Get number of symbols in the group. Console.WriteLine("Symbol Count: {0}", props.SymbolCount); // Get 8-bit parity data. Console.WriteLine("Parity: {0}", props.Parity); } } } }
Scan and recognize QR Code with logo image in C#
OnBarcode C# Barcode Reader library will automatically read and recognize QR Code with logo image in ASP.NET, Windows applications.
Process scanned QR Code data message using C#
QR Code supports encoding lots of character sets and some industry standard data formats. To help process these complex data message, barcode reader library
develops multiple methods to handle them.
Here you will learn how to process the following character sets or data message from scanned QR Code in C#
Here you will learn how to process the following character sets or data message from scanned QR Code in C#
- ASCII text. Including printing and non-printing chars.
- Unicode text
- Binary data
- GS1 data elements
ASCII text characters
ASCII text include 128 characters. The first 32 characters and the last character are non-printing ones, such as character 'carriage return'.
The following C# source code shows how to scan QR Code and parse the scanned ASCII characters using C#.
The following C# source code shows how to scan QR Code and parse the scanned ASCII characters using C#.
- Use method BarcodeScanner.Scan() to scan the image containing QR Code symbols.
- Parse the scanned QR Code data and replace non-printing char 'carriage return' in the example with visible label '[CR]'
String[] result = BarcodeScanner.Scan("C://Input//qrcode-ascii-sample.png", BarcodeType.QRCode); if (result.Length > 0) { foreach (String msg in result) { Debug.WriteLine("QR Code Raw Message: '" + msg + "'"); String tmp = msg.Replace("\r", "[CR]"); Debug.WriteLine("QR Code ASCII Text: '" + tmp + "'"); } } else { Debug.WriteLine("No QR Code Scanned!"); }
Unicode text
QR Code supports Unicode text encoding. To encode Unicode characters into QR Code, the barcode software
usually will convert Unicode text to binary data, and create QR Code with binary data using QR Code binary data mode.
To decode QR Code with Unicode text properly, you need use the same encoding as the QR Code generator. The most common encoding is using UTF8 encoding.
The C# code below explains how to read QR Code and decode Unicode text using UTF8 (System.Text.Encoding.UTF8) encoding in C# application.
To decode QR Code with Unicode text properly, you need use the same encoding as the QR Code generator. The most common encoding is using UTF8 encoding.
The C# code below explains how to read QR Code and decode Unicode text using UTF8 (System.Text.Encoding.UTF8) encoding in C# application.
BarcodeDetail[] datas = BarcodeScanner.ScanInDetails( "C://Input//qrcode-unicode.png", BarcodeType.QRCode); for (int j = 0; j < datas.Length; j++) { string textMsgDecoded = System.Text.Encoding.UTF8.GetString(datas[j].GetDataBytes()); Console.WriteLine("Unicode text: " + textMsgDecoded); }
GS1 data message
The GS1 System uses QR Code as one of its data carrier. The GS1 data message encoded in QR Code usually includes a list of AI (Application Identifier) Code and AI data pair, and some control characters, such as Function 1 Symbol Character (FNC1).
The OnBarcode Barcode Reader library will help you easily scan GS1 QR Code and parse GS1 data message without knowing any GS1 control characters. You can easily get the list of GS1 data elements.
The OnBarcode Barcode Reader library will help you easily scan GS1 QR Code and parse GS1 data message without knowing any GS1 control characters. You can easily get the list of GS1 data elements.
BarcodeDetail[] result = BarcodeScanner.ScanInDetails("C://Input//qrcode-gs1-data.png", BarcodeType.QRCode); foreach (BarcodeDetail b in result) { // Indicate if the QR Code is conform to GS1 system. if (b.IsGS1Compitable) { // Get message in string format. // Eg. "(415)5412345678908(3911)710125" String msg = b.GetMessage(); Console.WriteLine("Raw Data: '{0}'", b.Data); Console.WriteLine("Text Message: {0}", msg); // Retrieve each AI and its data from the message. // Eg. // AI: 415 // Data: 5412345678908 // AI: 3911 // Data: 710125 String[] vals = msg.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < vals.Length; i += 2) { Console.WriteLine("AI: {0}", vals[i]); Console.WriteLine("Data: {0}", vals[i + 1]); } } }
How to Verify Scanned QR Code using C# Barcode Reader library?
The OnBarcode C# QR Code reader library will automatically verify the detected QR Code data using QR Code decoding algorithm of the Reed-Solomon code.
During QR Code encoding process, the QR Code encoder (such as OnBarcode C# QR Code generator library) intentionally adds a set of specially designed redundant error correction codes.
During QR Code encoding process, the QR Code encoder (such as OnBarcode C# QR Code generator library) intentionally adds a set of specially designed redundant error correction codes.
The principle of error correction: Reed-Solomon codes
QR Codes use a mathematical algorithm called the Reed-Solomon code. When generating a QR Code, the encoder treats your original data (like a URL) as a mathematical polynomial. Then, based on the error correction level you choose, it generates another polynomial. This is the error correction code, and prints it right into the QR Code along with the original data.How "automatic error correction" works
When you scan a QR Code with barcode reader library, even if part of it is blocked, smudged, or damaged, the scanning library can still read the remaining intact portions. Then, the QR Code scanner library runs the decoding algorithm of the Reed-Solomon code:- It compares "the incomplete data read + the error correction code" with "what the data should theoretically look like."
- Using mathematical equations, it works backwards to figure out which parts are wrong and what the correct values should be.
- As long as the damaged area does not exceed the limit of the chosen error correction level, it can accurately reconstruct the original data.
Advanced QR Code Image Scanning Features using C#
OnBarcode Barcode Reader library provides several methods to improve the QR Code scanning speed.
- Quick to scan a single QR Code. If this feature is enabled, once the barcode library scan and detect a QR Code on the fil, it will stop run the remaining job.
- Scan QR Code and other barcode format at once
- Scan QR Codes from specified regions in the image file
Scan a single QR Code
If this feature is enabled, once the barcode library scan and detect a QR Code on the image file, it will stop run the remaining job.
This feature will really improve the reading speed, if the scanning file contains only one QR Code symbol.
This feature will really improve the reading speed, if the scanning file contains only one QR Code symbol.
string[] barcodes = BarcodeScanner.ScanSingleBarcode("qrcode-single-barcode.png", BarcodeType.QRCode);
Scan multiple QR Codes
You can easily scan QR Code and other barcode formats (such as Code 128, EAN/UPC) from an image file at once. Or you can scanned all barcode reader library supported barcode formats (using property BarcodeType.All) from a file.
BarcodeDetail[] datas = BarcodeScanner.ScanInDetails("qrcode-multiple-formats.png", new List<BarcodeType> { BarcodeType.QRCode, BarcodeType.Code128 });
BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails("qrcode-all-formats.png", BarcodeType.All);
Scan image regions to read QR Codes
To improve the reading speed and reduce the reading error, you can define regions inside the image file. The barcode reader library will scan the QR Code
from the specified regions, and it will help to improve the QR Code and other barcodes reading performance.
You can also apply single QR Code option with specified image areas using method
List<SRegion> regions = new List<SRegion>(); regions.Add(new SRegion(0, 0, 50, 60)); regions.Add(new SRegion(100, 100, 50, 60)); string[] barcodes = BarcodeScanner.ScanRegions("qrcode-barcodes.png", BarcodeType.QRCode, regions);
You can also apply single QR Code option with specified image areas using method
ScanSingleBarcodeRegions
List<SRegion> regions = new List<SRegion>(); regions.Add(new SRegion(0, 0, 50, 60)); regions.Add(new SRegion(100, 100, 50, 60)); string[] barcodes = BarcodeScanner.ScanSingleBarcodeRegions("qrcode-barcodes.png", BarcodeType.QRCode, regions);
Scan QR Code image with specified directions
The OnBarcode C# QR Code Scanner SDK supports scanning QR Codes from specified directions inside the image. By default, the barcode library will
scan the QR Codes from 4 different directions inside the image file:
To speed up the QR Code scanning performance, you can choose the specified scanning directions in C#. The following C# sample code, the library will scan the QR Code from two directions (left to right, and top to bottom).
- From left to right
- From right to left
- From top to bottom
- From bottom to top
To speed up the QR Code scanning performance, you can choose the specified scanning directions in C#. The following C# sample code, the library will scan the QR Code from two directions (left to right, and top to bottom).
ScanOptions ops = new ScanOptions(BarcodeType.QRCode); ops.ScanDirection = ScanDirectionType.LeftToRight | ScanDirectionType.TopToBottom; BarcodeDetail[] datasInDetail = BarcodeScanner.Scan("qrcode-sample.png", ops);
Scan QR Code image with specified step interval
Here we will explain how to configure the QR Code scanning step interval to improve the barcode reading speed.
When the C# QR Code Reader library processes raster image files for barcode detection, it parses image pixels line by line. You can control the scanning frequency via the
The following C# codes demonstrate how to scan QR Code with specified image scanning interval and direction.
When the C# QR Code Reader library processes raster image files for barcode detection, it parses image pixels line by line. You can control the scanning frequency via the
StepInterval property in the ScanOptions class.
- StepInterval (image scanning step interval):
Defines the line spacing for barcode scanning.
Minimum value: 1. The library scans every single line of the image.
Default value: 5. The library scans one line every 5 lines of the image.
The following C# codes demonstrate how to scan QR Code with specified image scanning interval and direction.
ScanOptions ops = new ScanOptions(BarcodeType.QRCode); ops.ScanDirection = ScanDirectionType.LeftToRight; ops.StepInterval = 5; BarcodeDetail[] datasInDetail = BarcodeScanner.Scan("qrcode-sample.png", ops);
Note: An overlarge value will reduce scanning accuracy and may cause missed barcodes.
Common Asked Questions
How to setup a C# ASP.NET or Windows project to read QR Codes?
To setup a new C# ASP.NET web or Windows application to scan QR Codes, you can create the new project using Visual Studio and download, install
C# Barcode Reader library from NuGet package manager. Enable QR Codes reading by inserting one single lines of C# code
BarcodeScanner.Scan() in your C# project.
What are the benefits of OnBarcode C# Barcode Reader library?
OnBarcode C# Barcode Reader library is a mature and reliable C# library to scan, decode QR Code, Code 128 and other 20+ 2d, 1d barcodes from image files.
It provides various scanning options to reduce barcode reading time, increase barcode reading ration, and supports various .NET projects and operating systems.
Is it possible to detect QR Codes from image file or image object in memory using C#?
Yes. You can scan and decode QR Codes from raster image files and image object in memory or database using C# Barcode Reader library.
How to read QR Code on desktop?
You can use barcode scanner software installed on computer or barcode scanner device to read and extract QR Code from image.
OnBarcode C# QR Code Reader library supports building Windows application with QR Code scanning and reading functions with few lines of C# source codes.
How to read a QR code that's on your screen?
To read a QR Code on your screen, try the following steps
- Take and save the screenshot of the QR Code image on the computer
- Use QR Code scanner software to read the QR Code from the image
How do I scan a QR code with my phone?
You can download and install QR Code Scanner app on the iPhone or Android phones. OnBarcode C# QR Code Scanner library supports building Xamarin application for iOS and Adroid with
QR Code reading functions.
How do you scan a QR code from an image?
Online QR Code decoder, QR Code Scanner software allow you to scan and extract a QR Code from an images.
Using OnBarcode QR Code Reader C# SDK, you can quickly build online QR Code scanner based on ASP.NET Core, MVC web app,
or Windows QR Code Scanner software using WinForms or WPF framework using C#.
Is there a scanner or reader for QR codes?
You can use iPhone and Android phone build-in QR Code Scanner app to read the QR Codes. If you are C#.NET developer, you can easily
build app for iOS and Android with QR Code Reader embed using C# Barcode Reader library with Xamarin.
Does C# Barcode Reader library support decoding QR Codes from video feeds?
Yes. C# Barcode Reader library supports scanning and decoding QR Codes from live video feeds. You can use 3rd party library to capture
image frames from live video feeds, and the barcode library will scan and recogize the QR Codes from them.
What extra libraries are necessary to create a QR Code reader C# ASP.NET web or WinForms desktop application?
C# Barcode Reader library includes all necessary libraries to detect and read QR Codes from image files in C# ASP.NET Core or Windows application.
If you need extract QR Codes from PDF, Microsoft Word, Excel, PowerPoint documents, you need other libraries to process and convert the documents to image
files in C# application.
How to get a free trial license of C# Barcode Reader?
You can get the free trial license package from OnBarcode website, or you can also download and install the trial package from NuGet Packge Manager.