我想使用 OpenCvSharp 和 ZXing.NET 在 C# 中解码不同的代码。图像捕获由 IDS 相机 (10 mp) 完成。我的代码如下所示:
using System;
using System.Drawing;
using OpenCvSharp;
using ZXing;
using ZXing.Datamatrix;
using ZXing.Multi;
using uEye;
class Program
{
static void Main(string[] args)
{
{
while (true)
{
// create new camera
Camera cam = new Camera();
cam.Init(1);
cam.Memory.Allocate();
// acquire image
cam.Acquisition.Capture(uEye.Defines.DeviceParameter.Wait);
// convert to byte-array (to show in window) and to bitmap (to decode)
cam.Memory.CopyToArray(1, out byte[] Imag);
cam.Memory.CopyToBitmap(1, out Bitmap Image);
// show image in window
Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(Image);
Cv2.Resize(mat, mat, new OpenCvSharp.Size(960, 687), 0.25, 0.25);
Cv2.ImShow("Code", mat);
Cv2.ResizeWindow("Code", 960, 687);
//BitMap --> LuminanceSource --> BinaryBitmap
LuminanceSource bild = new RGBLuminanceSource(Imag, 3840, 2748);
BinaryBitmap barcodeBitmap = new BinaryBitmap(new HybridBinarizer(bild));
// implement new reader and decode
var reader = new MultiFormatReader {};
var result = reader.decode(barcodeBitmap);
// write to console if decoded successfully, if not: write "failed"
if (result != null){
Console.WriteLine(result.Text);
}
else{
Console.WriteLine("failed");
}
int c = Cv2.WaitKey(0);
if (c != -1) { break; }
}
}
}
}
图像采集工作,但它不会解码。
我希望有人能帮助我。
编辑:如果有人遇到同样的问题。我回到 ZXing 并使用 AForge.NET 而不是 OpenCV。首先,我从相机内存中加载图像并将其转换为灰度。然后我使用阈值算法来获得 1 位图像。在此之后,我正在寻找四边形来获取代码区域并旋转图像,以便 ZXing 阅读器可以对其进行解码。实际上它适用于一维和数据矩阵代码。如果有人需要代码,请问我。