WP7 : Real-time video scan a barcode/QR code in your app using ZXing lib

 

 

WP7 : Real-time video scan a barcode/QR code in your app using ZXing lib

Rate This
  • Comments 21

Now that we can scan a barcode from a picture thanks to ZXing lib, let’s try to improve the user experience.

It would be much better to scan a barcode in real time, just like WP7 does out of the box. I wanted to provide this kind of experience in my own application. You can do the same by downloading the following lib and use the code below.

Get Microsoft Silverlight

 

Usage

Download the lib

Download the binaries and add a reference to both dlls in your own project.

Write the code

Use the video scan lib by calling the static StartScan method of the BarCodeManager static class.

/// <summary>
/// Starts the scan : navigates to the scan page and starts reading video stream
/// Note : Scan will auto-stop if navigation occurs
/// </summary>
/// <param name="onBarCodeFound">Delegate Action on a barcode found</param>
/// <param name="onError">Delegate Action on error</param>
/// <param name="zxingReader">(optional) A specific reader format, Default will be EAN13Reader </param>
public static void StartScan(Action<string> onBarCodeFound, Action<Exception> onError, BarcodeFormat barcodeFormat = null)

Parameters are:

- Action<string> onBarCodeFound : a delegate called on a background thread as soon as a barcode/QR code is detected. The parameter is the associated barcode/QR string 
- Action<Exception> onError : a delegate on error. The parameter is the associated exception that generated the error - BarcodeFormat barcodeFormat = null : the type of code that should be scanned (default = UPC_EAN): ALL_1D;
CODE_128;
CODE_39;
DATAMATRIX;
EAN_13;
EAN_8;
ITF;
PDF417;
QR_CODE;
UPC_A;
UPC_E;
// Auto detect
UPC_EAN;

Calling StartScan will launch the WP7 camera, and start the scanning process which consists in:

  1. start focusing
  2. once focused, try scanning during 1.5 sec

If unsuccessful, it will retry 15 times after what it will call the onError callback with a VideoScanException exception as a parameter. You can update the trial count by setting BarCodeManager.MaxTry (default is 15).

If a code was found, the onBarCodeFound callback will be called with the string code found as a parameter.

Example

image

Scan a bar code

private void Button_Click(object sender, RoutedEventArgs e)
{
    WP7.ScanBarCode.BarCodeManager.StartScan(
        // on success
        (b) => Dispatcher.BeginInvoke(() => 
            {
                tbScanResultBarCode.Text = b;
                NavigationService.GoBack();
            }),
        // on error
        (ex) => Dispatcher.BeginInvoke(() => 
            {
                tbScanResultBarCode.Text = ex.Message;
                NavigationService.GoBack();
            })
        // Default : please, decode any bar-code
        );
                                 
}

Scan a QR code

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    WP7.ScanBarCode.BarCodeManager.StartScan(
        // on success
        (b) => Dispatcher.BeginInvoke(() => 
            {
                tbScanResultQR.Text = b;
                NavigationService.GoBack();
            }),
        // on error
        (ex) => Dispatcher.BeginInvoke(() => 
            {
                tbScanResultQR.Text = ex.Message;
                NavigationService.GoBack();
            }),
        // Please, decode a QR Code
        BarcodeFormat.QR_CODE);
}
 

Get the source code and test the sample application

You can test the sample application ang get the whole code here.

The lib uses the video capabilities of WP7.1 SDK with the PhotoCamera class. The code is inspired from the original idea of Pierre Cauchois and includes some snippets from SLAR toolkit.

[This is an early version made for my own purpose and was not tested for production]

Leave a Comment
  • Please add 3 and 2 and type the answer here:
  • Post
  • Please ,anyone help me , that WP7.videoScanZXing work on windows phone 8 ?

  • Great post!

    but

    I try to test it on my wp7 device but when i tap on button ("tap to scan a barcode") the app will crash.

  • Hi! im a beginner for Mobile App...i got your App but when i tried to deploy this app on my Windows 7 OD Lumia it show and error that the device is locked. Can anyone help me out for this bcoz i really need to test it on my device and wanted to work on it futher. reply ASAP Plz.

  • Hello,

    To those of you who have deployed the sample on your devices and get the following reader Exception on this statement "result = BarCodeManager.ZXingReader.decode(binaryBitmap)"

    It is because the code states that it should work with 13 bar barcodes: // Default : please, decode a 13 bar-code. (The first place I saw it was in MainPage.xaml.cs)

    I realized that otherwise it won't work.

  • Hi,

    Great Tut !

    I want to make an offline OCR. Is it possible to do it with this lib ?

  • Does this work on Windows phone 8 ?

Page 2 of 2 (21 items) 12
Page 2 of 4 (83 items) 1234