0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

using Zxing.Net with .Net5 or higher(.Net6)

Posted at

Example

There is a difference between using Zxing.Net with .Net4.x or lower and that with .Net5 or heigher
Introducing some of them, a "BarcodeReader" the function reading a barcode or QRcode is able to use all of .Net version.
In .Net4.x or lower, we can use BarcodeReader like this

Zxing.BarcodeWriter? barcode = new BarcodeWriter;
Zxing.Result res = barcode.decode(new Bitmap("BitmapImage"));
string result = res.Text;

But in .Net5 or heigher, "BarcodeWriter"is type of generic so we have to use "QRCodeReader".
There is some difficult point in "QRCodeReader".
Some of them, type of QRCodeReader's argument is BinaryBitmap.It is not useful.
In this article,we will making a system that can be Making or Reading a QRCode and set result in "string result" in easy and short way

Decode

Nuget:

Zxing.Net
ZXing.Net.Bindings.SkiaSharp

var txt = string.Empty;
string path = "xxx.jpg";
var stream = System.IO.File.Open(path, System.IO.FileMode.Open);
using (var skiaImage = SkiaSharp.SKBitmap.Decode(stream))
{
    var skiaReader = new ZXing.SkiaSharp.BarcodeReader();
    var skiaResult = skiaReader.Decode(skiaImage);
    txt = skiaResult?.Text ?? imgUrl;
    string result = skiaResult.tostring();
}

Maybe we should confirm that skiaResult isn't null

by

Encode

Encode and set a propaty"Source" of "ResultImage"

Nuget:

Zxing.Net
ZXing.Net.Bindings.SkiaSharp

var qrCode = new ZXing.SkiaSharp.BarcodeWriter() {
    Format = ZXing.BarcodeFormat.QR_CODE,
    Options = new QrCodeEncodingOptions {
        Height = (int)ResultImage.Height,
        Width = (int)ResultImage.Width,
        Margin = 0
    }
};
var skBitmap = qrCode.Write(result);
using (var image = SKImage.FromBitmap(skBitmap)) {
    using (var data = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100)) {
        var source = BitmapFrame.Create(data.AsStream());
        ResultImage = source;
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?