gpt4 book ai didi

OpenCvSharp with ZXing 用摄像头读取条码

转载 作者:行者123 更新时间:2023-12-02 17:18:03 32 4
gpt4 key购买 nike

我在将 Zxing 库与 openCvSharp 集成时遇到问题。我正在尝试使用相机实时读取条形码。
它在 Result result = barcodeReader.Decode(test); 行抛出异常。给;

System.InvalidOperationException: 'You have to declare a delegate which converts your byte array to a luminance source object.'
如果有人有任何解决方案、建议或想法,我将不胜感激!
完整代码是:
static void Main(string[] args)
{

VideoCapture capture = new VideoCapture(0);
LuminanceSource source;

using (Window window = new Window("Camera"))

using(Mat image = new Mat())
{

while (true)
{
capture.Read(image); // same as cvQueryFrame

BarcodeReader barcodeReader = new BarcodeReader();

var barcodeBitMap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

byte[] test;

using (var stream = new MemoryStream())
{
barcodeBitMap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
test = stream.ToArray();
}

Result result = barcodeReader.Decode(test);

if (result != null)
{
Console.WriteLine("result: " + result.Text);
}

window.ShowImage(image);

Cv2.WaitKey(30);
}
}
}

最佳答案

根据 .Net 目标平台,您可以使用 Decode 方法直接使用位图。只有 .Net 核心/标准版本不直接支持位图类。

    static void Main(string[] args)
{
VideoCapture capture = new VideoCapture(0);

BarcodeReader barcodeReader = new BarcodeReader();

using (Window window = new Window("Camera"))
using(Mat image = new Mat())
{
while (true)
{
capture.Read(image); // same as cvQueryFrame

var barcodeBitMap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

Result result = barcodeReader.Decode(barcodeBitMap);

if (result != null)
{
Console.WriteLine("result: " + result.Text);
}

window.ShowImage(image);

Cv2.WaitKey(30);
}
}
}
或者,您可以尝试以下绑定(bind)之一
https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCV/
https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCVSharp.V2/
使用它们,您可以实例化另一个直接支持 Mat 结构的 BarcodeReader 实现。
    static void Main(string[] args)
{
VideoCapture capture = new VideoCapture(0);

BarcodeReader barcodeReader = new ZXing.OpenCV.BarcodeReader();

using (Window window = new Window("Camera"))
using(Mat image = new Mat())
{
while (true)
{
capture.Read(image); // same as cvQueryFrame

Result result = barcodeReader.Decode(image);

if (result != null)
{
Console.WriteLine("result: " + result.Text);
}

window.ShowImage(image);

Cv2.WaitKey(30);
}
}
}

关于OpenCvSharp with ZXing 用摄像头读取条码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63707116/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com