英文:
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<Rgba32> image = SixLabors.ImageSharp.Image.Load<Rgba32>(byteArray);
// Create an instance of ImageSharpLuminanceSource with Rgba32 pixel format
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论