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 is made for clean images: generated, printed or scanned barcodes, photographed straight on. 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. Code 39, Codabar and ITF, which have no check character, must be crossed by two lines that agree. A transparent background reads as white.
A barcode needs its quiet zones and narrow bars of at least 2 pixels. The pixels are classified as dark or light with the Otsu threshold of the image, then with a local threshold (Sauvola) when no barcode is found.
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));