Search code examples
c#xamluwpzxing

Limit scan area in Zxing.Net


I built a UWP XAML control that acts as a barcode/qrcode scanner using the zxing.net library (http://zxingnet.codeplex.com/). The control works fine, it previews the camera on the device and then captures a frame and let zxing process it. All a user has to do is to place it in a page and tell it what type of barcode to scan.

I am just facing one problem: How can I limit the scan area to the center of the captured frame? Sometimes there are multiple barcodes in the image and the library returns a result from one of the barcodes but I am interested in the barcode that is in the middle of the frame.

Is this possible with zxing.net? If so, how can I limit the scan area?


Solution

  • I don't know what code are you using. But I can give a hint based on my UWP barcode scanner

    Inside CapturePhotoFromCameraAsync() Task you can find code that take "screenshot" frame from camera:

     VideoFrame videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)_width, (int)_height);
     await mediaCapture.GetPreviewFrameAsync(videoFrame);
    

    You can get there SoftwareBitmap and eben convert to WritableBitmap.

     SoftwareBitmap sb = videoFrame.SoftwareBitmap;
     WriteableBitmap bitmap = new WriteableBitmap(sb.PixelWidth, sb.PixelHeight);
    

    But now there is another question how to crop WriteableBitmap (you can find solution on SO or MSDN - it's not short) and how to convert back to SoftwareBitmap.