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]