Search code examples
c#wpfbarcodewebcambarcode-scanner

How to use the Zxing barcode scanner library along with the Aforge webcam library


I have a program in which I use the Aforge library for viewing a webcam. This works wonder:

LocalWebcamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
LocalScannerBarcode = new VideoCaptureDevice(LocalWebcamsCollection[WebcamNumber].MonikerString);
LocalScannerBarcode.NewFrame += LocalScannerBarcode_NewFrame;
LocalScannerBarcode.Start();

and in the new frame event I get the bitmap

System.Drawing.Bitmap frame;    
void LocalScannerBarcode_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    frame = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
}

now I have to decode what is seen. Basically I have to pass the bitmap to decode. So global I have;

ZXing.BarcodeReader bcr;

and into the event LocalScannerBarcode_NewFrame

if (bcr == null)
        bcr = new ZXing.BarcodeReader();

but as soon as I put the two lines above the event is not called anymore.

Please notice that in Windows forms that works but I have to do it in WPF.

Thanks


Solution

  • Not sure if this helps but have you tried putting the reference to the ZXing library in another project? Something an Helper.

    So in you project you will have:

    string strResult = Helper.ReadBarcode(frame);
    if (strResult != null)
    {
                ... do stuff with the string
    }
    

    and in the helper

    static ZXing.BarcodeReader bcr;
    public static string ReadBarcode(System.Drawing.Bitmap bmp)
    {
        if (bcr == null)
            bcr = new ZXing.BarcodeReader();
        return bcr.Decode(bmp).ToString();
    }