WP7 : Real-time video scan a barcode/QR code in your app using ZXing lib
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.
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:
- start focusing
- 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
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]
Comments
Anonymous
December 07, 2011
Is it possible to scan a barcode in portrait mode?Anonymous
December 20, 2011
Hello Tim, this version only supports landscape modeAnonymous
January 18, 2012
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.Anonymous
January 19, 2012
Hi reena, The downloaded source code should be working well. Can you contact me by email for further investigation ?Anonymous
January 23, 2012
Any attempt to create a Win7 store app? I have interest in a home inventory app to:
- scan groceries for home inventory and integrate to shopping list
- scan equipment for insurance list
- up load to skydrive to export inventory I'm willing to Angel fund this Ideas? Thanks Kevin
Anonymous
February 01, 2012
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 :-)Anonymous
February 01, 2012
Hi, It´s possible scan a DataMatrix Barcode? I´m using its but not works. What can I do for this? Thanks, JohnAnonymous
February 07, 2012
Hello, I experienced the same problem with ReaderException as reena. Have you managed to investigate it? Thanks in advance.Anonymous
February 07, 2012
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éphanieAnonymous
April 29, 2012
is there a way to display the qr focus area on the camera screen ?Anonymous
May 17, 2012
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??Anonymous
May 22, 2012
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??Anonymous
June 10, 2012
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.Anonymous
June 11, 2012
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?Anonymous
December 03, 2012
Tt looks this example doesn't work on Windows Phone 8, any ideas?Anonymous
December 24, 2012
Please ,anyone help me , that WP7.videoScanZXing work on windows phone 8 ?Anonymous
May 02, 2013
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.Anonymous
May 13, 2013
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.Anonymous
May 29, 2013
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.Anonymous
August 16, 2013
Hi, Great Tut ! I want to make an offline OCR. Is it possible to do it with this lib ?Anonymous
November 26, 2013
Does this work on Windows phone 8 ?