From a7bf6fc2c049271dcf7344b430006493594c3132 Mon Sep 17 00:00:00 2001 From: huserben Date: Sat, 9 Nov 2013 12:38:54 +0100 Subject: [PATCH] added windows phone version --- plugin.xml | 29 ++++++++- src/wp/BarcodeScanner.cs | 46 +++++++++++++ src/wp/Result.cs | 23 +++++++ src/wp/Scan.xaml | 45 +++++++++++++ src/wp/Scan.xaml.cs | 136 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 src/wp/BarcodeScanner.cs create mode 100644 src/wp/Result.cs create mode 100644 src/wp/Scan.xaml create mode 100644 src/wp/Scan.xaml.cs diff --git a/plugin.xml b/plugin.xml index 194fcfa..1a6d8cf 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.1.0"> BarcodeScanner Scans Barcodes. @@ -37,7 +37,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/wp/BarcodeScanner.cs b/src/wp/BarcodeScanner.cs new file mode 100644 index 0000000..e8d6a9e --- /dev/null +++ b/src/wp/BarcodeScanner.cs @@ -0,0 +1,46 @@ +using BarcodeScanner; +using Microsoft.Phone.Controls; +using System; +using System.Windows; +using System.Windows.Navigation; +using WPCordovaClassLib.Cordova; +using WPCordovaClassLib.Cordova.Commands; +using WPCordovaClassLib.Cordova.JSON; + +namespace Cordova.Extension.Commands +{ + public class BarcodeScanner : BaseCommand + { + PhoneApplicationFrame currentRootVisual; + public void scan(string options) + { + Deployment.Current.Dispatcher.BeginInvoke(() => + { + currentRootVisual = Application.Current.RootVisual as PhoneApplicationFrame; + currentRootVisual.Navigated += frame_Navigated; + currentRootVisual.Navigate(new Uri("/Plugins/com.phonegap.plugins.barcodescanner/Scan.xaml", UriKind.Relative)); + }); + } + + void frame_Navigated(object sender, NavigationEventArgs e) + { + var scanPage = e.Content as Scan; + if (scanPage != null) + { + scanPage.BarcodePlugin = this; + } + } + + internal void ResultReceived(BarcodeScannerResult scanResult) + { + var resultString = JsonHelper.Serialize(scanResult); + + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultString)); + } + + internal void ScanFailed(string error) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, error)); + } + } +} diff --git a/src/wp/Result.cs b/src/wp/Result.cs new file mode 100644 index 0000000..33115db --- /dev/null +++ b/src/wp/Result.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BarcodeScanner +{ + public class BarcodeScannerResult + { + public string format + { + get; + set; + } + + public string text + { + get; + set; + } + } +} diff --git a/src/wp/Scan.xaml b/src/wp/Scan.xaml new file mode 100644 index 0000000..359d34f --- /dev/null +++ b/src/wp/Scan.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/wp/Scan.xaml.cs b/src/wp/Scan.xaml.cs new file mode 100644 index 0000000..5c88760 --- /dev/null +++ b/src/wp/Scan.xaml.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Navigation; +using Microsoft.Phone.Controls; +using Microsoft.Phone.Shell; +using Microsoft.Devices; +using System.Windows.Threading; +using System.Windows.Media.Imaging; +using ZXing; + +namespace BarcodeScanner +{ + public partial class Scan : PhoneApplicationPage + { + private PhotoCamera _phoneCamera; + private IBarcodeReader _barcodeReader; + private DispatcherTimer _scanTimer; + private WriteableBitmap _previewBuffer; + + public Scan() + { + InitializeComponent(); + } + + public Cordova.Extension.Commands.BarcodeScanner BarcodePlugin + { + get; + set; + } + + protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) + { + // Initialize the camera object + _phoneCamera = new PhotoCamera(); + _phoneCamera.Initialized += cam_Initialized; + + CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed; + + //Display the camera feed in the UI + viewfinderBrush.SetSource(_phoneCamera); + + + // This timer will be used to scan the camera buffer every 250ms and scan for any barcodes + _scanTimer = new DispatcherTimer(); + _scanTimer.Interval = TimeSpan.FromMilliseconds(250); + _scanTimer.Tick += (o, arg) => ScanForBarcode(); + + base.OnNavigatedTo(e); + } + + void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e) + { + _phoneCamera.Focus(); + } + + protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) + { + //we're navigating away from this page, we won't be scanning any barcodes + _scanTimer.Stop(); + + if (_phoneCamera != null) + { + // Cleanup + _phoneCamera.Dispose(); + _phoneCamera.Initialized -= cam_Initialized; + CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed; + } + } + + void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e) + { + if (e.Succeeded) + { + this.Dispatcher.BeginInvoke(delegate() + { + _phoneCamera.FlashMode = FlashMode.Off; + _previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height); + + _barcodeReader = new BarcodeReader(); + + // By default, BarcodeReader will scan every supported barcode type + // If we want to limit the type of barcodes our app can read, + // we can do it by adding each format to this list object + + //var supportedBarcodeFormats = new List(); + //supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE); + //supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX); + //_bcReader.PossibleFormats = supportedBarcodeFormats; + + _barcodeReader.Options.TryHarder = true; + + _barcodeReader.ResultFound += _bcReader_ResultFound; + _scanTimer.Start(); + }); + } + else + { + Dispatcher.BeginInvoke(() => + { + MessageBox.Show("Unable to initialize the camera"); + }); + } + } + + void _bcReader_ResultFound(Result obj) + { + // If a new barcode is found, vibrate the device and display the barcode details in the UI + if (!obj.Text.Equals(tbBarcodeData.Text)) + { + var barcodeScannerResult = new BarcodeScannerResult(); + barcodeScannerResult.format = obj.BarcodeFormat.ToString(); + barcodeScannerResult.text = obj.Text; + + BarcodePlugin.ResultReceived(barcodeScannerResult); + NavigationService.GoBack(); + } + } + + private void ScanForBarcode() + { + //grab a camera snapshot + _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels); + _previewBuffer.Invalidate(); + + //scan the captured snapshot for barcodes + //if a barcode is found, the ResultFound event will fire + _barcodeReader.Decode(_previewBuffer); + + } + + } +} \ No newline at end of file