Reputation: 1
I want to decode different codes in C# by using OpenCvSharp and ZXing.NET. The Image-Capture is done by an IDS-Camera (10 mp). My code looks like this:
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; }
}
}
}
}
The image-acquisition works but it won't decode.
I hope somebody can help me.
Edit: Just if somebody is having the same Problem. I came back to ZXing and instead of OpenCV i used AForge.NET. First i load the image from the camera-memory and convert it into Grayscale. Then im using an thresholding-algorithm to get an 1bit-image. After this i am looking for quadrilaterales to get the code area and rotate the image so that the ZXing-reader can decode it. Actually its working fine for 1D- and datamatrix codes. If somebody needs the code, just ask me.
Upvotes: 0
Views: 840