Click or drag to resize

Decode Barcode from Image

This is a simple example to read a barcode from an image file and decode its content.

C#
using Opait.Barcoder.Api;

// Decodes the specified barcode image file.
static void DecodeImageBarcode(string fileName)
{
    // Initialize the barcoder library with default options.
    var barcoder = new Barcoder();

    // Open the image file.
    using (var bitmap = Image.FromFile(fileName) as Bitmap)
    {
        // Create a decoder class.
        var decoder = barcoder.CreateDecoder();

        // Detect a single barcode within the image.
        var result = decoder.Decode(bitmap);

        // Check for detection failure.
        if (result == null)
        {
            Console.WriteLine("Unable to find a barcode.");
            return;
        }

        // Display results.
        Console.WriteLine($"Barcode Type: {result.BarcodeType}");
        Console.WriteLine($"Barcode Text: {result.Text}");
    }
}