在网上找了找扫描二维码的例子,利用ZXing库实现(下载),提供的Silverlight版本的下载,在网上搜了一下已经有wp的dll可用了,不过网上实现的条码扫描的例子还都是用的Silverlight版本的dll。有个例子感觉写的不错能学到东西,就模仿着写了写。
public static class BarCodeManager
{
internal static Action<string> BarCodeFound;
internal static Action<Exception> Error; static BarCodeManager()
{ } /// <summary>
/// 启动条码扫描
/// </summary>
/// <param name="barCodeFound">扫描成功执行的操作</param>
/// <param name="error">扫描失败的操作</param>
/// <param name="barcodeFormat">编码类型,默认为ean_13条形码</param>
public static void StartScan(Action<string> barCodeFound, Action<Exception> 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<string> 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<CameraOperationCompletedEventArgs>(photocamera_Initialized);
photocamera.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(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();
}
/// <summary>
/// 获取摄像头扫描的图像
/// </summary>
/// <returns></returns>
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!纯属来帖代码来了,,,,,,