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 4 and 2 and type the answer here:
  • Post
  • Is it possible to scan a barcode in portrait mode?

  • Hello Tim, this version only supports landscape mode

  • Hi,

    Its urgent for me.Please help out from this.I have taken downloaded same code but every time its displaying error as (reader Exception on this statement "result = BarCodeManager.ZXingReader.decode(binaryBitmap)".I am unable to find out the error.

    Desperately waiting for your reply.

    Advance thanks.

  • Hi reena,

    The downloaded source code should be working well.

    Can you contact me by email for further investigation ?

  • Any attempt to create a Win7 store app?  I have interest in a home inventory app to:

    1. scan groceries for home inventory and integrate to shopping list

    2. scan equipment for insurance list

    3. up load to skydrive to export inventory

    I'm willing to Angel fund this

    Ideas?

    Thanks

    Kevin

  • Hi, thank you for this blog.

    I have tried your code. Unfortunatly, all I have right now is the emulator...  is there a solution for that scenario ?

    Hoping to see you at the Techdays on 08 Feb 2012 à Paris :-)

  • Hi,

    It´s possible scan a DataMatrix Barcode?

    I´m using its but not works.

    What can I do for this?

    Thanks,

    John

  • Hello,

    I experienced the same problem with ReaderException as reena. Have you managed to investigate it?

    Thanks in advance.

  • Hi all,

    At this point, the lib works well when deployed on the device. It does not support emulator mode as it need your video stream including the barcode/QR code.

    If you have any trouble with this sample/lib, it might be because:

    1 - you didn't deploy it on a device

    2 - you did not install the latest Windows Phone SDK

    I updated the source code with a warning messagebox when working on the emulator.

    If you checked the 2 points mentionned earlier, and it still doens't work, please contact me by email.

    Stéphanie

  • is there a way to display the qr focus area on the camera screen ?

  • Hi, thank you for the sample application, but in the sample, if i press back key in the scanning view, it will throw an exception, any idea to solve it??

  • nice post.  It works well for your code.  However, if I want to cancel the scanning, it will crashed if I press the physical back button.  How can we avoid this??

  • Many thanks for this sample project. I found it very helpful in learning how to deal with the camera and capturing a barcode automatically.

    Merci beaucoup.

  • I've got a few QrCodes printed on paper which do work really well with the integrated bing scan&search capability of windows phone.

    Pity is, they hardly work with your sample app. It takes ages until they are recognised, and usually only after I rotate the phone a few times around, if at all.

    Is this a limitation of the zxing lib or might something in your wrapper be the problem?

  • Tt looks this example doesn't work on Windows Phone 8, any ideas?

Page 1 of 2 (21 items) 12
Page 1 of 4 (87 items) 1234