BarcodeImageReader Class
Provides methods to read the linear barcodes of an image: Code 39, Code 93, Code 128, GS1-128, EAN, UPC, Codabar, ITF and GS1 DataBar.
public static class BarcodeImageReaderMethods
| Name | Description |
|---|---|
| ReadBarcodes(Image, BarcodeReadingOptions?, CancellationToken) | Reads the linear barcodes of an image. |
ReadBarcodes(Image, BarcodeReadingOptions?, CancellationToken)
BarcodeImageReader.csReads the linear barcodes of an image.
public static IReadOnlyList<BarcodeImageResult> ReadBarcodes(this Image image, BarcodeReadingOptions? options = null, CancellationToken cancellationToken = default)Parameters
imageImage- The image. For an animated image, its first frame is read.
optionsBarcodeReadingOptions?- The reading options, or
nullfor the defaults. cancellationTokenCancellationToken- A token to cancel the reading.
Returns
IReadOnlyList<BarcodeImageResult> The barcodes found, each once; empty when there is none.
Exceptions
ArgumentNullExceptionimageisnull.OperationCanceledExceptioncancellationTokenwas canceled.
Remarks
The reader reads generated, printed and scanned images, and photographs: blurred, unevenly lit, slightly tilted. Parallel scan lines cross the image horizontally, vertically and at 30°, 60°, 120° and 150°, and BarcodeDecoder.DecodeScanLines reads the barcodes they cross: a barcode turned by another angle is read when one of the lines crosses all its bars. The edges of the bars are first found in the grayscale image, between the pixels, with a threshold that follows the lighting; then the image classified as dark or light (a local threshold, then the Otsu threshold of the image) is read along the same lines. A transparent background and the outside of the image read as white: a barcode cropped at its first and last bars has its quiet zones.
A barcode needs narrow bars of about 2 pixels or more, and two scan lines that agree. Code 39, Codabar and ITF, whose check is weak or missing, must be read over a height of at least 4% of their length; a Codabar symbol needs 4 characters or more (start and stop characters included), an ITF symbol 6 digits or more.
Example
using var image = Barcode.CreateEan13("590123412345").ToImage(); // or Image.Load("product.jpg")
var results = image.ReadBarcodes();
var result = Assert.Single(results);
Assert.Equal((BarcodeType.Ean13, "5901234123457"), (result.Result.Type, result.Text));