baltermia / barcodereader-imagesharp

A barcode reader compatible with SixLabors.ImageSharp using ZXing. Trying to remove System.Drawing.Common dependencies.
MIT License
8 stars 3 forks source link
barcode dotnet imagesharp zxing

BarcodeReader.ImageSharp

[![Nuget](https://img.shields.io/nuget/v/BarcodeReader.ImageSharp?style=flat-square)](https://www.nuget.org/packages/BarcodeReader.ImageSharp/) [![Downloads](https://img.shields.io/nuget/dt/BarcodeReader.ImageSharp.svg?style=flat-square)](https://www.nuget.org/packages/BarcodeReader.ImageSharp/) ![Build Status](https://img.shields.io/github/actions/workflow/status/baltermia/barcodereader-imagesharp/dotnet.yml?style=flat-square) A barcode reader compatible with SixLabors.ImageSharp using ZXing. Trying to remove System.Drawing.Common dependencies.

I created this library specifically to use with blazor but you can totally use this for other use cases!

Features

This is a basic facade-library for the ZXing.Bindings.ImageSharp.V2 library which uses SixLabors.ImageSharp.

Included Features are:

.NET Support

The library supports a vast majority of up-to-date .NET versions:

Installation

There are two ways to add the BarcodeReader.ImageSharp library to your projects:

  1. Open the command line and go into the directoy where your .csproj file is located, then execute this command:

    dotnet add package BarcodeReader.ImageSharp
  2. Or add it in the GUI of Visual Studio 20XX:
    Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution...


Then add the following using to your C# files:

using BarcodeReader.ImageSharp;

And depending on your code, you probably need one or more of the following usings:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Formats.Png;

How to Use

In the following code we use one of ImageSharps pixelformats IPixel<Rgba32> which we will use to load PNG images from files.

string path = @"C:\images\qrcode.png";

// first we need to Load a image
Image<Rgba32> image = Image.Load<Rgba32>(path);

// then we create a new BarcodeReader object
BarcodeReader<Rgba32> reader = new BarcodeReader<Rgba32>(types: ZXing.BarcodeFormat.QR_CODE);

// then we can get a result by decoding
BarcodeResult<Rgba32> result = await reader.DecodeAsync(image);

// then we can print out the result
if (result.Status == Status.Found)
{
    Console.WriteLine(result.Value);
}
else if (result.Status == Status.NotFound)
{
    Console.WriteLine(result.Message);
}
else if (result.Status == Status.Error)
{
    Console.WriteLine("An error occured while decoding barcode");
}

If you're using the reader in a while loop, you can use the DetectedBarcode event to only recieve BarcodeResults with a Status.Found:

// add handler to event
reader.DetectedBarcode += Recieved_Handler;

// infite loop for demonstration purposes
while (true)
{
    // recieve image from any method (not defined here)
    Image<Rgba32> img = GetNewImage();

    // decode the image. DetectedBarcode events are fired when a barcode could be found
    await reader.DecodeAsync(img);
}

// handler which is called when a barcode could be detected by any Decode call
void Recieved_Handler(object sender, BarcodeEventArgs<Rgba32> e) { /* Do something with the result */ }