博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows phone 扫描二维码
阅读量:5095 次
发布时间:2019-06-13

本文共 6637 字,大约阅读时间需要 22 分钟。

在网上找了找扫描二维码的例子,利用ZXing库实现(),提供的Silverlight版本的下载,在网上搜了一下已经有wp的dll可用了,不过网上实现的条码扫描的例子还都是用的Silverlight版本的dll。有个例子感觉写的不错能学到东西,就模仿着写了写。

public static class BarCodeManager    {        internal static Action
BarCodeFound; internal static Action
Error; static BarCodeManager() { } ///
/// 启动条码扫描 /// ///
扫描成功执行的操作 ///
扫描失败的操作 ///
编码类型,默认为ean_13条形码 public static void StartScan(Action
barCodeFound, Action
error, BarcodeFormat barcodeFormat = null) { var mainFrame = Application.Current.RootVisual as PhoneApplicationFrame; if (mainFrame != null) { if (barcodeFormat == null) { barcodeFormat = BarcodeFormat.EAN_13; } BarCodeFound = barCodeFound; Error = error; ZXingReader = GetReader(barcodeFormat); mainFrame.Navigate(new Uri("/ScanCode.xaml", UriKind.Relative)); } } private static Reader zXingReader; public static Reader ZXingReader { get { if (zXingReader == null) { return new EAN13Reader(); } return zXingReader; } set { BarCodeManager.zXingReader = value; } } internal static Reader GetReader(BarcodeFormat format) { Reader r; switch (format.Name) { case "CODE_128": r = new Code128Reader(); break; case "CODE_39": r = new Code39Reader(); break; case "EAN_13": r = new EAN13Reader(); break; case "EAN_8": r = new EAN8Reader(); break; case "ITF": r = new ITFReader(); break; case "UPC_A": r = new UPCAReader(); break; case "UPC_E": r = new UPCEReader(); break; case "QR_CODE": r = new QRCodeReader(); break; default: r = null; break; } return r; } }
Action<>这个第一次接触到,要扫描条码信息就调用类的StartScan方法,跳转到ScanCode.xaml 启动扫描。通过第一个参数(Action
barCodeFound) 返回扫面的结果。
ScanCode.cs代码
public partial class ScanCode : PhoneApplicationPage    {        PhotoCamera photocamera;        VideoBrush videoBrush = new VideoBrush();        byte[] buffer = null;//存储photocamera 获取的亮度数据        Result result = null;        public ScanCode()        {            InitializeComponent();        }        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            if (photocamera == null)            {                photocamera = new PhotoCamera();                photocamera.Initialized += new EventHandler
(photocamera_Initialized); photocamera.AutoFocusCompleted += new EventHandler
(photocamera_AutoFocusCompleted); video.Fill = videoBrush; videoBrush.SetSource(photocamera); } base.OnNavigatedTo(e); } protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { if (photocamera != null) { //删除事件处理 msdn备注 photocamera.Initialized -= photocamera_Initialized; photocamera.AutoFocusCompleted -= photocamera_AutoFocusCompleted; photocamera.CancelFocus(); photocamera.Dispose(); } base.OnNavigatingFrom(e); }    //对焦操作完成发生 void photocamera_AutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e) { result = null; try { while (result == null) { var binaryBitmap = GetBitmapFromCamera(); if (binaryBitmap != null) { try { result = BarCodeManager.ZXingReader.decode(binaryBitmap); } catch { } } } if (result != null) { BarCodeManager.BarCodeFound(result.Text); } else {
            //再次执行相机聚焦操作,也可以用DispatcherTimer周期执行GetBitmapFromCamera获取解码数据 photocamera.Focus(); } } catch (Exception ex) { //BarCodeManager.Error(ex); } } void photocamera_Initialized(object sender, CameraOperationCompletedEventArgs e) { photocamera.FlashMode = FlashMode.Auto; photocamera.Focus(); } ///
/// 获取摄像头扫描的图像 /// ///
BinaryBitmap GetBitmapFromCamera() { BinaryBitmap binaeryBitmap = null; try { var pixelWidth = (int)photocamera.PreviewResolution.Width; var pixelHeight = (int)photocamera.PreviewResolution.Height; if (buffer == null || buffer.Length != (pixelHeight * pixelWidth)) { buffer = new byte[pixelWidth * pixelHeight]; } //捕捉亮度数据,创建HybridBinarizer和BinaryBitmap类,返回BinaryBitmap给Reade对象解码 photocamera.GetPreviewBufferY(buffer); var luminance = new RGBLuminanceSource(buffer, pixelWidth, pixelHeight, true); var binarizer = new HybridBinarizer(luminance); binaeryBitmap = new BinaryBitmap(binarizer); } catch { } return binaeryBitmap; } }

调用代码,调用StartScan,完成条码扫描,所有的操作完全可以写在一个ScanCode类中,这种设计是可以在封装成一个dll,在外部直接调用

BarCodeManager.StartScan(                (r) => Dispatcher.BeginInvoke(() =>                    {                        tbScanResultBarCode.Text = r;                        NavigationService.GoBack();                    }),                 null                );

OK!纯属来帖代码来了,,,,,,

转载于:https://www.cnblogs.com/my-tzc/p/3266122.html

你可能感兴趣的文章
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
android中自定义下拉框(转)
查看>>
Android设计模式源码解析之外观模式(Facade)
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
Hmailserver搭建邮件服务器
查看>>
django之多表查询-2
查看>>
BULK INSERT, 实战手记:让百万级数据瞬间导入SQL Server
查看>>
快速幂
查看>>
改善C#公共程序类库质量的10种方法
查看>>
AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
查看>>
MyBaits动态sql语句
查看>>