I'm having a problem with integrating Zxing libary with openCvSharp. I'm trying to read barcodes live with camera.
It throws an exception at the line Result result = barcodeReader.Decode(test);
that gives;
System.InvalidOperationException: 'You have to declare a delegate which converts your byte array to a luminance source object.'
I would appreciate If someone have any solution, suggestion or idea! Full code is:
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);
}
}
}
Depending on the .Net target platform you can use the bitmap directly with the method Decode. Only the .Net core/standard versions don't support the Bitmap classes directly.
Alternatively you can try out one of the following bindings
https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCV/
https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCVSharp.V2/
With them you can instantiate another BarcodeReader implementation which supports the Mat struct directly.