世界上信息无处不在,条码就无孔不入。方便快速的生成和读取条码信息,已经成为智能手机的基本功能。如何在Window Phone手机应用上实现条码扫描功能呢?最先想到的可能是首先用摄像头读取到条码图像、二值化、图像处理等等,让人头大。但是俗话说的好,Don't Reinvent The Wheel,要感谢强大的ZXing库[1],我们可以把精力多放在app本身功能的实现上。ZXing库实现了众多的一维/二维条码的读取功能,例如常见的EAN-13和QR码。Zxing支持多种编程语言,有人实现了Silverlight版本[2],WP7应用中可以很方便的使用。本文从一个实例入手,介绍一下如何来使用ZXing。

简单起见,封装一个页面Barcode,从任何页面切换到Barcode页面,均自动开始条码读取,读取完成后,切换到结果页面。封装上[3]其实做的更好些,但是无奈在我的app中使用不便,[3]非常值得参考。

一、引用ZXing

建立WP7BarcodeScannerExample实例工程,从[2]下载SilverlightZxing库,添加对Silverlight_ZXing_Core.dll的引用。下面一段截取的代码,可以看出ZXing的使用是比较方便的。

using com.google.zxing;using com.google.zxing.oned;using com.google.zxing.qrcode;using com.google.zxing.common;namespace WP7BarcodeScannerExample{    public partial class BarCode : PhoneApplicationPage    {        private Reader _reader = new EAN13Reader();//or QRCodeReader()        private void ScanPreviewBuffer()        {
Result result
= _reader.decode(binBitmap); if (result != null) { //读取成功,结果是result.Text } } }}

 首先,根据需要读取的条码类型,以EAN-13一维条码为例,初始化一个EAN13Reader,然后对获取到的图像binBitmap进行decode,即可得到结果。

 

二、PhotoCamera取景

[4]非常详细的说明了PhotoCamera的使用方法,这里就不再赘述了。注意wp7模拟器是不支持摄像头的,所以该实例必须在真机上运行。

 

三、Barcode页面

主要思路是:使用VideoBrush将PhotoCamera取景显示出来,按照固定的间隔时间进行对焦并读取图片,调用zxing的制定编码类型的×××进行解码,成功后停止取景和读取,切换至结果页面。

Barcode.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">        <Rectangle HorizontalAlignment="Stretch" Name="frame" Stroke="Black" StrokeThickness="0" VerticalAlignment="Stretch">            <Rectangle.Fill>                <VideoBrush x:Name="_videoBrush">                    <VideoBrush.RelativeTransform>                        <CompositeTransform                              x:Name="_previewTransform" CenterX=".5" CenterY=".5" />                    VideoBrush.RelativeTransform>                VideoBrush>            Rectangle.Fill>        Rectangle>Grid>

注意VideoBrush,因为PhotoCamera取景默认是横屏的,需要对其做一下变换。

 

Barcode.xaml.cs:

using System;using System.IO;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;using Microsoft.Devices;using com.google.zxing;using com.google.zxing.oned;using com.google.zxing.qrcode;using com.google.zxing.common;using System.Windows.Threading;using System.Windows.Media.Imaging;using System.Windows.Navigation;namespace WP7BarcodeScannerExample{    public partial class BarCode : PhoneApplicationPage    {        private PhotoCamera _photoCamera;        private PhotoCameraLuminanceSource _luminance;        private readonly DispatcherTimer _timer;        //×××        private Reader _reader = null;        public BarCode()        {            InitializeComponent();            _timer = new DispatcherTimer();            _timer.Interval = TimeSpan.FromMilliseconds(250);            //间隔250ms调用读取函数            _timer.Tick += (o, arg) => ScanPreviewBuffer();        }        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            string type = "";                        if (NavigationContext.QueryString.TryGetValue("type", out type) && type == "qrcode")            {                _reader = new QRCodeReader();            }            else            {                _reader = new EAN13Reader();            }            _photoCamera = new PhotoCamera();            _photoCamera.Initialized += new EventHandler(cam_Initialized);            _videoBrush.SetSource(_photoCamera);            base.OnNavigatedTo(e);        }        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)        {            if (_photoCamera != null)            {                _timer.Stop();                _photoCamera.CancelFocus();                _photoCamera.Dispose();            }                        base.OnNavigatingFrom(e);        }        void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)        {            int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);            int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);            _luminance = new PhotoCameraLuminanceSource(width, height);                        Dispatcher.BeginInvoke(() =>            {                _previewTransform.Rotation = _photoCamera.Orientation;                _timer.Start();            });            _photoCamera.FlashMode = FlashMode.Auto;            _photoCamera.Focus();        }        private void ScanPreviewBuffer()        {            try            {                _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);                var binarizer = new HybridBinarizer(_luminance);                var binBitmap = new BinaryBitmap(binarizer);                Result result = _reader.decode(binBitmap);                if (result != null)                {                    _timer.Stop();                    Dispatcher.BeginInvoke(() =>                    {                        //读取成功,结果存放在result.Text                    });                }                else                 {                    _photoCamera.Focus();                }            }            catch            {            }        }    }}

 注意:

1,重点查看ScanPreviewBuffer()函数,调用_reader对二值化后的图像进行解码,若result非空,则表示解码成功,结果存放在result.Text中,可以使用多种方法传递到你应用的其他页面中。

2,Navigate到Barcode页面时,若不带querystring参数,则默认使用EAN13Reader进行一维条码识别。若带type=qrcode,则使用QRCodeReader进行二维条码识别。

 

四、体验

总体上,该实例的条码扫描过程,速度是比较快的。但是在扫描过程需要反复对焦,在光线等条件不好时,速度相对较慢。为了更好的体验,条码扫描过程需要增加一些辅助功能,例如在取景页面上显示一个指示框,初始是红色,读取成功后变为绿色;读取成功后,停止取景,获取当前的一张图片进行显示,防止直接切换至结构页面有些突兀。例子中这些内容实现的不好,仅仅是作为例子,多种可能,由你来完成了。

 

五、实例代码

实例代码存放在github,地址:https://github.com/gzb1985/WP7BarcodeScannerExample,欢迎批评指正,真诚希望能对大家有所帮助。

 

六、引用

[1]:ZXing, http://code.google.com/p/zxing/

[2]:Silverlight ZXing,Windows Phone 7 Silverlight ZXing Barcode Scanning Library, http://silverlightzxing.codeplex.com/

[3]:Stephanie Hertrich,WP7 : Real-time video scan a barcode/QR code in your app using ZXing lib,http://blogs.msdn.com/b/stephe/archive/2011/11/07/wp7-real-time-video-scan-a-barcode-qr-code-in-your-app-using-zxing-lib.aspx

[4]:Matt Stroshane,Using Cameras in Your Windows Phone Application,http://msdn.microsoft.com/en-us/magazine/hh708750.aspx

[5]:QR code scanning on Windows Phone 7.5 using ZXlib,http://jonas.follesoe.no/2011/07/22/qr-code-scanning-on-windows-phone-75-using-zxlib/