Read QR code from byte using ZXing.Net on .net7

huangapple go评论63阅读模式
英文:

Read QR code from byte using ZXing.Net on .net7

问题

以下是翻译好的代码部分:

这是我的代码

    public static string ReadQRCode(byte[] imageBytes) 
    {
        var barcodeReader = new BarcodeReader();
        barcodeReader.Options.TryHarder = true; 
        barcodeReader.Options.PossibleFormats = new[] { BarcodeFormat.QR_CODE };
    
        var result = barcodeReader.Decode(imageBytes);
        if (result != null) {
            return result.Text;
        }
    
        return null;
    }

但然后我收到了这个错误信息

> 使用通用类型 'BarcodeReader<T>' 需要 1 个类型参数

请指导我如何解决这个问题
英文:

This is my code

public static string ReadQRCode(byte[] imageBytes) 
{
    var barcodeReader = new BarcodeReader();
    barcodeReader.Options.TryHarder = true; 
    barcodeReader.Options.PossibleFormats = new[] { BarcodeFormat.QR_CODE };

    var result = barcodeReader.Decode(imageBytes);
    if (result != null) {
        return result.Text;
    }

    return null;
}

But then I got this error message

> Using the generic type 'BarcodeReader<T>' requires 1 type arguments

Please guide me to solve the problem

答案1

得分: 2

以下是翻译好的部分:

第一个例子使用

ZXing.Net.Bindings.Windows.Compatibility
请注意,此包仅适用于 Windows。

using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;

public static string ReadQRCode(byte[] byteArray)
{
    Bitmap target;
    target = ByteToBitmap(byteArray);
    var source = new BitmapLuminanceSource(target);
    var bitmap = new BinaryBitmap(new HybridBinarizer(source));
    var reader = new QRCodeReader();
    var result = reader.decode(bitmap);
    return result.Text;
}

public static Bitmap ByteToBitmap(byte[] byteArray) 
{
    Bitmap target;
    using (var stream = new MemoryStream(byteArray)) {
         target = new Bitmap(stream);
    }

     return target;
}

第二个例子使用

ZXing.Net.Bindings.ImageSharp.V2

using ZXing;
using ZXing.Common;
using ZXing.ImageSharp;
using ZXing.QrCode;

public static string ReadQRCode(byte[] byteArray) 
{
    // 从字节数组加载图像
    Image<Rgba32> image = SixLabors.ImageSharp.Image.Load<Rgba32>(byteArray);
    // 使用 Rgba32 像素格式创建 ImageSharpLuminanceSource 的实例
    var luminanceSource = new ImageSharpLuminanceSource<Rgba32>(image);

    var bitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
    var reader = new QRCodeReader();
    var result = reader.decode(bitmap);
    return result.Text;
}
英文:

This is how I solve it
First you need to install one of the packages from (ZXing.Net.Bindings.*)
First example using
> ZXing.Net.Bindings.Windows.Compatibility
Please note this package only works on Windows.

using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;
public static string ReadQRCode(byte[] byteArray)
{
    Bitmap target;
    target = ByteToBitmap(byteArray);
    var source = new BitmapLuminanceSource(target);
    var bitmap = new BinaryBitmap(new HybridBinarizer(source));
    var reader = new QRCodeReader();
    var result = reader.decode(bitmap);
    return result.Text;
}

public static Bitmap ByteToBitmap(byte[] byteArray) 
{
    Bitmap target;
    using (var stream = new MemoryStream(byteArray)) {
         target = new Bitmap(stream);
    }

     return target;
}

Second example using
> ZXing.Net.Bindings.ImageSharp.V2

using ZXing;
using ZXing.Common;
using ZXing.ImageSharp;
using ZXing.QrCode;
    public static string ReadQRCode(byte[] byteArray) 
    {
        // Load image from byte array
        Image&lt;Rgba32&gt; image = SixLabors.ImageSharp.Image.Load&lt;Rgba32&gt;(byteArray);
        // Create an instance of ImageSharpLuminanceSource with Rgba32 pixel format
        var luminanceSource = new ImageSharpLuminanceSource&lt;Rgba32&gt;(image);

        var bitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
        var reader = new QRCodeReader();
        var result = reader.decode(bitmap);
        return result. Text;
    }

huangapple
  • 本文由 发表于 2023年3月4日 02:51:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定