Skip to content

Commit

Permalink
dotnet: improve README (simpler example, why ZXingCpp section)
Browse files Browse the repository at this point in the history
  • Loading branch information
axxel committed Feb 2, 2024
1 parent e6fb84b commit 66b47cc
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions wrappers/dotnet/ZXingCpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,27 @@ It is an open-source, multi-format linear/matrix barcode image processing librar
It was originally ported from the Java ZXing Library but has been developed further and now includes
many improvements in terms of runtime and detection performance.


## Usage

```cs
using System.Collections.Generic;
using ImageMagick;
using SkiaSharp;
using ZXingCpp;

// BarcodeReader extension class to support direct reading from MagickImage
public static class MagickImageBarcodeReader
{
public static List<Barcode> Read(MagickImage img, ReaderOptions? opts = null)
{
if (img.DetermineBitDepth() < 8)
img.SetBitDepth(8);
var bytes = img.ToByteArray(MagickFormat.Gray);
var iv = new ImageView(bytes, img.Width, img.Height, ImageFormat.Lum);
return BarcodeReader.Read(iv, opts);
}

public static List<Barcode> Read(this BarcodeReader reader, MagickImage img)
=> Read(img, reader);
}

public class Program
{
public static void Main(string[] args)
{
var img = new MagickImage(args[0]);
var img = SKBitmap.Decode(args[0]).Copy(SKColorType.Gray8);
var iv = new ImageView(img.GetPixels(), img.Info.Width, img.Info.Height, ImageFormat.Lum);

var reader = new BarcodeReader() {
Formats = BarcodeReader.FormatsFromString(args[1]),
Formats = args.Length > 1 ? BarcodeReader.FormatsFromString(args[1]) : BarcodeFormats.Any,
TryInvert = false,
// see the ReaderOptions implementation for more available options
};

foreach (var b in reader.Read(img))
foreach (var b in reader.Read(iv))
Console.WriteLine($"{b.Format} : {b.Text}");
}
}
Expand All @@ -51,14 +36,44 @@ To run the code above, it is important that the dotnet runtime finds the native
`ZXing[.dll|.so|.dylib]` in your path. E.g. on Linux a complete command line would look like this

```sh
LD_LIBRARY_PATH=<ZXing.so-path> dotnet run -- <image-file-name>
LD_LIBRARY_PATH=<ZXing.so-path> dotnet run -- <image-file-name> [barcode-format-list]
```

Note: This is an alpha release, meaning the API may still change slightly to feel even more
"managed" depending on community feedback.
Note: This is an alpha release, meaning the API may still change slightly to potentially feel even
more like a native C# library depending on community feedback.


## Why ZXingCpp?

There are a number of areas where ZXingCpp shines compared to other popular .NET barcode scanner libraries.
The following comparison is with respect to the open source [ZXing.Net](https://www.nuget.org/packages/ZXing.Net)
and the commercial [Dynamsoft](https://www.nuget.org/packages/Dynamsoft.DotNet.Barcode) projects.

### Performance

To compare the performance of ZXingCpp with the other two libraries, I started the project
[zxing-bench](https://github.com/axxel/zxing-bench).
The [README](https://github.com/axxel/zxing-bench/blob/main/dotnet/README.md) contains a few details but to get
an idea: ZXingCpp is on average 2x-10x faster than Dynamsoft and 10x-50x faster than ZXing.Net.

### Detection rate

The benchmarking tool also showed that ZXingCpp has a superior detection rate compared to ZXing.Net while it is
sometimes better sometimes worse than the commercial Dynamsoft package, depending on the sample type and the
library configuration. The latter definitively supports more barcode formats compared to the two ZXing decendents.

### Ease of use

The sample program above shows the simplicitly of the API. The others are similar but seem a bit more
complicated with regards to setting parameters.

### Standards support

ZXingCpp has full support for binary data and ECI handling and provides a standards conforming `bytesECI()`
data that can be used to simulate a hardware/handheld barcode scanner. This seems not the case for ZXing.Net
and is unclear for Dynamsoft.

## Benchmarking
### License / costs

To compare the performance of this .NET wrapper project with other available barcode scanner .NET libraries,
I started the project [zxing-bench](https://github.com/axxel/zxing-bench).
The [README](https://github.com/axxel/zxing-bench/blob/main/dotnet/README.md) contains a few results to get an idea.
ZXingCpp has the liberal Apache-2.0 license and is free to use in commercial applications. That said,
I accept [donations](https://github.com/sponsors/axxel) and might be available for commercial consulting ;).

0 comments on commit 66b47cc

Please sign in to comment.