How to read Barcodes from Image Files (jpg, png, gif, tiff, svg, bmp)
How to read barcodes from Image in C#
Install with NuGet
Install-Package BarCode
Download DLL
Manually install into your project
Install with NuGet
Install-Package BarCode
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronBarcode on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming with C#.
Install-Package BarCode
Consider installing the IronBarcode DLL directly. Download and manually install it for your project or GAC form: IronBarCode.zip
Manually install into your project
Download DLLRead Barcodes from Image Directly
One of the key feature of IronBarcode is its ability to read barcodes out of the box from multiple image formats. This includes:
* Scalable Vector Graphics (SVG)
* Joint Photographic Experts Group (JPEG)
* Portable Network Graphics (PNG)
* Graphics Interchange Format (GIF)
* Tagged Image File Format (TIFF)
* Bitmap Image File (BMP)
This is made possible with the help of our open source library, IronDrawing. Now let us see how can we use IronBarcode to read barcodes from two barcode image attached as demonstrated in the code snippet below.
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-images-1.cs
using IronBarCode;
using System;
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}
Imports IronBarCode
Imports System
Private myBarcode = BarcodeReader.Read("image_file_path.jpg") 'image file path
For Each item In myBarcode
Console.WriteLine(item.ToString())
Next item
Want to know what are the barcode values in the samples? Try it with the code snippet!!
To use IronBarcode, the first thing you have to do is to install IronBarcode library via Microsoft Visual Studio NuGet package manager into your project like shown in the picture below. This will allow you to access IronBarcode BarcodeReader.Read()
method to directly read barcode image.
From the code snippet above, we can see that IronBarcode offers simplicity by allowing users to only use BarcodeReader.Read()
to read an image file that has already been included inside the project by specifying the file name string, OR file path string as the parameter for the method. Best practice is to use the verbatim string literal, "@" when specifying a file path in the method as not including this would require users to add multiple escape characters "\" in the file path string.
Attach Values()
method at the end of BarcodeReader.Read()
method call to get the barcode value as a System.String []
object.
To get the result showing in console, you can use foreach
loop to iterate on the values stored in the string []
array, and inside the loop block, call Console.WriteLine()
method with the iterator variable as the parameter.
This method does not only able to read 1-Dimensional barcode formats(Codabar, Code128, Code39, Code93, EAN13, EAN18, ITF, MSI, UPCA, UPCE), it also able to read 2-Dimensional barcode formats(Aztec, DataMatrix, QRCode) in various image formats.
Setting Barcode Reader Options
Feeling barcode reading too slow? Barcode too small in the picture and IronBarcode cannot read? Want to read only certain areas of an image? Want to read only certain type of barcodes in an image with mixtures of barcodes? Want to improve overall reading performance? Worry no more!
BarcodeReaderOptions
allows users to tweak or adjust the behavior of the barcode reader so that it can address all the issues stated above. Let us see and discuss in detail all the adjustable properties available in BarcodeReaderOptions
one-by-one:
CropArea
CropArea
is a property of type IronSoftware.Drawing.CropRectangle
available in BarcodeReaderOptions
that allows users to specify the area in an image that the user want IronBarcode to read. This will help in improving the reading performance, since the barcode reader does not need to scan through the entire image for barcodes, as well as improving reading accuracy since the area of read has been specified.
To set the CropArea
property, simply instantiate a new Rectangle type object and specify the rectangle coordinates, width, and length of the rectangle as arguments. The measurement unit accepted is pixels(px).
CropArea = new IronSoftware.Drawing.Rectangle(x, y, width, height)
ExpectBarcodeTypes
By default, all supported barcodes in IronBarcode will be scanned in an image. However, if the user know what type of barcodes are available or want to be read in an image, setting this property to read only a certain types of barcode will greatly increase reading performance and accuracy since the barcode reader does not need to iterate through collections of barcodes to interpret and read a barcode.
To use this property, simply set the ExpectBarcodeTypes
to one of the fields of BarcodeEncoding
enum. Let us learn on every barcode types supported by IronBarcode and look at a sample barcode from every type.
- AllOneDimensional : Linear types of Barcode. This includes Codabar, Code128, Code39, Code93, EAN13, EAN18, ITF, MSI, UPCA, UPCE barcodes.
- AllTwoDimensional : This includes Grid, Matrix, and Stacked Barcodes. The barcode type that falls under 2 Dimensional barcodes are Aztec, DataMatrix, QRCode barcodes.
- Aztec : Aztec 2D barcode format. Aztec Code is a type of 2D barcode invented by Andrew Longacre, Jr. and Robert Hussey in 1995. Named after the resemblance of the central finder pattern to an Aztec pyramid, Aztec code has the potential to use less space than other matrix barcodes because it does not require a surrounding blank "quiet zone". Below is an example of an Aztec Barcode
- Codabar : Codabar is a linear barcode symbology developed in 1972 by Pitney Bowes Corp. Codabar encodes numerical data (digits) only. Below is an example of Codabar barcode
- Code128 : Code 128 is a high-density linear barcode symbology defined in ISO/IEC 15417:2007. It is used for alphanumeric or numeric-only barcodes. Below is an example of Code128 barcode
- Code39 : Code 39 is a variable length, discrete barcode symbology. The Code 39 specification defines 43 characters, consisting of uppercase letters (A through Z). Below is an example of a Code39 barcode
- Code93 : Code 93 1D barcode format. Code 93 is a barcode symbology designed in 1982 by Intermec to provide a higher density and data security enhancement to Code 39. Code 93 supports encoding of only the following ASCII characters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 - . $ / + % SPACE. Below is an example of a Code93 barcode
- DataMatrix : A Data Matrix is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern, also known as a matrix. The information to be encoded can be text or numeric data. Usual data size is from a few bytes up to 1556 bytes. Below is an example of a DataMatrix barcode
- EAN13 : The International Article Number (also known as European Article Number or EAN) is a standard describing a barcode symbology and numbering system used in global trade to identify a specific retail product type, in a specific packaging configuration, from a specific manufacturer. EAN-13 may only encode numerical (digits) content of length 12 or 13 digits long. Shorter Barcodes will have trailing zeros (000) prepended to the start of the number automatically. Below is an example of EAN13 barcode
- EAN8 : An EAN-8 is an EAN/UPC symbology barcode and is derived from the longer International Article Number (EAN-13) code. EAN-8 may only encode numerical (digits) content of length 7 or 8 digits long. Shorter Barcodes will have trailing zeros (000) prepended to the start of the number automatically. Below is an example of EAN8 barcode
- IntelligentMail : Intelligent Mail 2D barcode format. The Intelligent Mail Barcode (Also known as "IM Barcode" or "USPS OneCode Barcodes" or "IMB") is a 65-bar barcode for use on mail in the United States. The term "Intelligent Mail" refers to services offered by the United States Postal Service for domestic mail delivery. The IM barcode is intended to provide greater information and functionality than its predecessors POSTNET and PLANET. Please note that IronBarcode can only READ this type of barcode. Below is an example of barcode of this type
- ITF : ITF-14 is the GS1 implementation of an Interleaved 2 of 5 (ITF) bar code to encode a Global Trade Item Number. ITF-14 symbols are generally used on packaging levels of a product, such as a case box of 24 cans of soup. The ITF-14 will always encode 14 digits. ITF encodes numerical data only. If the number if digits is not even, a '0' will automatically be prepended. Below is an example of ITF barcode
- MaxiCode : MaxiCode 2D barcode format. MaxiCode is a public domain, machine-readable symbol system originally created and used by United Parcel Service. Suitable for tracking and managing the shipment of packages, it resembles a barcode, but uses dots arranged in a hexagonal grid instead. Please note that IronBarcode can only READ this type of barcode. Below is an example of barcode of this type
- MSI : MSI is a barcode symbology developed by the MSI Data Corporation, based on the original Plessey Code symbology. This type of barcode only accepts numeric values. Below is an example of an MSI type barcode
- PDF417 : PDF417 is a stacked linear barcode symbol format used in a variety of applications, primarily transport, identification cards, and inventory management. PDF stands for Portable Data File. The 417 signifies that each pattern in the code consists of 4 bars and spaces, and that each pattern is 17 units long. The PDF417 symbology was invented by Dr. Ynjiun P. Wang at Symbol Technologies in 1991. (Wang 1993) It is ISO standard 15438. Below is an example of PDF417 barcode
- PharmaCode : Pharmaceutical Binary Code. A reading fault tolerant binary barcode standard used in the medical industry. Please note that IronBarcode can only READ this type of barcode. Below is an example of a PharmaCode barcode
- Plessey : Plessey Code is a 1D linear barcode symbology based on pulse width modulation, developed in 1971 by The Plessey Company PLC, a British-based company. This barcode type only accepts numeric values. Below is an example of a Plessey barcode
- QRCode : QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to efficiently store data; extensions may also be used. Below is an example of a QR code barcode
- Rss14 : Reduce Space Symbology 14 barcode format. May represent a 1D barcode or Stacked 2D barcode. RSS 14 barcode (Reduce Space Symbology) encodes the full 14-digit EAN.UCC item identification in a symbol that can be omni-directionally scanned by suitably configured point-of-sale laser scanners. It is the latest barcode types for space-constrained identification from EAN International and the Uniform Code Council, Inc. RSS barcodes have been identified to target the grocery industry and in healthcare, where items are too small to allow for other barcode symbologies. Please note that IronBarcode can only READ this type of barcode. Below is an example of Rss14 barcode
- UPCA : The Universal Product Code (UPC) is a barcode symbology that is widely used in the United States, Canada, United Kingdom, Australia, New Zealand, in Europe and other countries for tracking trade items in stores. UPC (technically refers to UPC-A) consists of 12 numeric digits, that are uniquely assigned to each trade item. Along with the related EAN barcode, the UPC is the barcode mainly used for scanning of trade items at the point of sale, per GS1 specifications. UPCA may only encode numerical (digits) content of length 12 or 13 digits long. Shorter Barcodes will have trailing zeros (000) prepended to the start of the number automatically. Below is an example of a UPCA barcode
- UPCE : To allow the use of UPC barcodes on smaller packages, where a full 12-digit barcode may not fit, a 'zero-suppressed' version of UPC was developed, called UPC-E, in which the number system digit, all trailing zeros in the manufacturer code, and all leading zeros in the product code, are suppressed. UPCE may only encode numerical (digits) content of length 7 or 8 digits long. Below is an example of UPCE barcode
ExpectMultipleBarcodes
IronBarcode will scan all barcodes available in an image by default, which includes scanning the whole image file and adding the read barcode values into the string array. However, if users do not wish to read multiple barcodes in an image file, users can set this property to false
which will make the barcode reader to stop scanning once a barcode value has been found. This will again, improve the performance and reading speed of IronBarcode.
ImageFilters
One of the properties that can be added in BarcodeReaderOptions
is a collection of image filters. Image filters are important to preprocess the raw image been fed to Iron Barcode. To apply image filters inside the BarcodeReaderOptions
, users must first initiate and specify the ImageFilter
collection to be used.
MaxParallelThreads
IronBarcode also allow users to enable and tweak the amount of parallel threads execution, which in turn will improve the speed and efficiency of the process. Parallel threads means execution of mutiple threads simultaneously on different processors core. Default amount for MaxParallelThread
property in IronBarcode is 4. Users can adjust them based on the capabilities and amount of resource their machines have.
Multithreaded
This property enables IronBarcode to read multiple images in parallel. The default for Multihreaded
is True, hence the multiple threads will be managed automatically to improve performance for batch barcode reading tasks.
RemoveFalsePositive
This property removes any false positive barcode reads. False positive barcode reads simply means a false read of barcode values, but identified as valid. This can happen due to errors in the sequencing process, such as sequencing errors, or errors in the barcode labeling or preparation process. Therefore, setting RemoveFalsePositive
as true will remove the false positive barcode readings, thus improving barcode reading accuracy. However, if users ought for performance at the cost of accuracy, setting this property to False would help. Default value for this property is True.
Speed
As the name suggest, Speed
enable users to further optimize the performance of IronBarcode barcode reader. Same as RemoveFalsePositive
property, tweaking this property will improve performance at the cost of accuracy, but in 4 levels as shown below:
ReadingSpeed.Faster
Setting the
Speed
property to this value will make the reading of barcodes fastest, however, at the expense of accuracy. The program will finish execute fast, but giving empty barcode result most of the time. This is because the input image are not preprocessed at all and IronBarcode reads the barcode image as is. Users are adivsed to use this value only if they are confident that the input image is sharp and clear.ReadingSpeed.Balanced
This setting is recommended for the
Speed
property. It sets a balance between accuracy and read performance by attempting to apply Light Processing to the image to clarify the barcode area and make it stand out for the barcode reader to detect. Most of the time, this setting is enough for IronBarcode to read a barcode image and produce accurate output.ReadingSpeed.Detailed
In case using the setting
ReadingSpeed.Balanced
is not successful in producing barcode value from the read, users may opt to useReadingSpeed.Detailed
. IronBarcode will perform a Medium Processing on the image to clarify the barcode area further and clearer for the barcode reader to detect the barcode. This setting is very useful to detect a small or less sharp barcode image.Please note however, that this setting is CPU-intensive and may affect read performance. Users are encouraged to experiment with other settings before using this setting. Combination of
ReadingSpeed.Detailed
withRemoveFalsePositive
set to "True" will print a warning to the console. However, it will not affect read and can be ignored.ReadingSpeed.ExtremeDetail
This setting is the least recommended setting due to its CPU-intensive process, whereby Heavy Processing will be performed on the barcode image in order for the reader to be able to read the barcodes. This will greatly reduce the reading performance of IronBarcode. Users are advised to do the image preprocessing/applying filters on the image before opt for this setting.
Please note however, that this setting is CPU-intensive and may affect read performance. Users are encouraged to experiment with other settings before using this setting. Combination of
ReadingSpeed.ExtremeDetail
withRemoveFalsePositive
set to "True" will print a warning to the console. However, it will not affect read and can be ignored.
UseCode39ExtendedMode
This setting allows Code39 type barcodes to be read and interpreted with extended mode whereby the full ASCII Character Set will be applied. Setting UseCode39ExtendedMode
to True will enable a more accurate reading of Code39 barcodes.
Advance Barcode Read from Image
Now that we have learned all options that can be tweaked by users, be it to increase performance or accuracy, let us see how we can apply it in our code. The code snippet below demonstrates.
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-images-2.cs
using IronBarCode;
using System;
BarcodeReaderOptions myOptions = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional, //or AllTwoDimensional
ExpectMultipleBarcodes = true, // Default is true
MaxParallelThreads = 2, // Default is 4
Speed = ReadingSpeed.Detailed, // 4 levels of speed. Default is Balanced
CropArea = new IronSoftware.Drawing.Rectangle(x: 242, y: 1124, width: 359, height: 378), // Units are in px
ImageFilters = new ImageFilterCollection { new BinaryThresholdFilter() }, // Assign to image filter object name
Multithreaded = true, // Default is true
UseCode39ExtendedMode = true, // Default is true
};
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg", myOptions); // Image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}
Imports IronBarCode
Imports System
Private myOptions As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
.ExpectMultipleBarcodes = True,
.MaxParallelThreads = 2,
.Speed = ReadingSpeed.Detailed,
.CropArea = New IronSoftware.Drawing.Rectangle(x:= 242, y:= 1124, width:= 359, height:= 378),
.ImageFilters = New ImageFilterCollection From {New BinaryThresholdFilter()},
.Multithreaded = True,
.UseCode39ExtendedMode = True
}
Private myBarcode = BarcodeReader.Read("image_file_path.jpg", myOptions) ' Image file path
For Each item In myBarcode
Console.WriteLine(item.ToString())
Next item
From the code snippet, we can see that in order to use BarcodeReaderOptions
we have to first initialized it, then we can determine and adjust the properties of the BarcodeReaderOptions
according to the properties stated above. The initialized BarcodeReaderOptions
can then later be used as an argument in BarcodeReader.Read()
method along with the image file. This will apply all the settings in BarcodeReaderOptions
when reading barcode from the image.