From ba3c96c38609dad0314096c3beb1093b87f7bfb1 Mon Sep 17 00:00:00 2001 From: Ryan Willoughby Date: Thu, 26 Sep 2013 17:17:00 -0700 Subject: [PATCH 1/5] [js] plugin.xml asset backwards compat --- plugin.xml | 4 +++- www/barcodescanner.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 9d88546..194fcfa 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.0.2"> BarcodeScanner Scans Barcodes. @@ -15,6 +15,8 @@ + + diff --git a/www/barcodescanner.js b/www/barcodescanner.js index 42b587c..a86990a 100644 --- a/www/barcodescanner.js +++ b/www/barcodescanner.js @@ -6,6 +6,7 @@ * Copyright (c) 2011, IBM Corporation */ + var ScannerLoader = function (require, exports, module) { var exec = require("cordova/exec"); @@ -85,3 +86,12 @@ var barcodeScanner = new BarcodeScanner(); module.exports = barcodeScanner; + } + + ScannerLoader(require, exports, module); + + cordova.define("cordova/plugin/BarcodeScanner", ScannerLoader); + + + + From a7bf6fc2c049271dcf7344b430006493594c3132 Mon Sep 17 00:00:00 2001 From: huserben Date: Sat, 9 Nov 2013 12:38:54 +0100 Subject: [PATCH 2/5] 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 From eab15a370a418ad15a87c84e817a15540131d6ff Mon Sep 17 00:00:00 2001 From: huserben Date: Sat, 9 Nov 2013 20:35:15 +0100 Subject: [PATCH 3/5] Updated Plugin, refactored some library parts, added description to read me --- README.md | 5 + plugin.xml | 4 +- src/wp/BarcodeScanner.cs | 24 ++--- src/wp/{Result.cs => BarcodeScannerResult.cs} | 0 src/wp/Scan.xaml | 5 - src/wp/Scan.xaml.cs | 91 +++++++++++-------- 6 files changed, 72 insertions(+), 57 deletions(-) rename src/wp/{Result.cs => BarcodeScannerResult.cs} (100%) diff --git a/README.md b/README.md index a3965d6..5bf55ae 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ BarcodeScanner ============== +Added version that runs with windows mobile devices. +To run on those devices, the zxing.net library must be added to the project (Install as nuget packet - more information on http://www.nuget.org/packages/ZXing.Net) + +Windows Phone currently only supports Scaning for codes, feel free to extend it that it can also encode. + Cross-platform BarcodeScanner for Cordova / PhoneGap. Follows the [Cordova Plugin spec](https://github.com/apache/cordova-plugman/blob/master/plugin_spec.md), so that it works with [Plugman](https://github.com/apache/cordova-plugman). diff --git a/plugin.xml b/plugin.xml index 1a6d8cf..7b1e774 100644 --- a/plugin.xml +++ b/plugin.xml @@ -48,7 +48,7 @@ - + @@ -61,7 +61,7 @@ - + diff --git a/src/wp/BarcodeScanner.cs b/src/wp/BarcodeScanner.cs index e8d6a9e..a685de9 100644 --- a/src/wp/BarcodeScanner.cs +++ b/src/wp/BarcodeScanner.cs @@ -11,36 +11,36 @@ namespace Cordova.Extension.Commands { public class BarcodeScanner : BaseCommand { - PhoneApplicationFrame currentRootVisual; + private PhoneApplicationFrame currentRootVisual; + public void scan(string options) { Deployment.Current.Dispatcher.BeginInvoke(() => { currentRootVisual = Application.Current.RootVisual as PhoneApplicationFrame; - currentRootVisual.Navigated += frame_Navigated; + currentRootVisual.Navigated += OnFrameNavigated; currentRootVisual.Navigate(new Uri("/Plugins/com.phonegap.plugins.barcodescanner/Scan.xaml", UriKind.Relative)); }); } - void frame_Navigated(object sender, NavigationEventArgs e) + private void OnFrameNavigated(object sender, NavigationEventArgs e) { var scanPage = e.Content as Scan; if (scanPage != null) { - scanPage.BarcodePlugin = this; + scanPage.BarcodeScannerPlugin = this; } } - internal void ResultReceived(BarcodeScannerResult scanResult) - { - var resultString = JsonHelper.Serialize(scanResult); - - DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultString)); - } - - internal void ScanFailed(string error) + public void OnScanFailed(string error) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, error)); } + + public void OnScanSucceeded(BarcodeScannerResult scanResult) + { + var resultString = JsonHelper.Serialize(scanResult); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultString)); + } } } diff --git a/src/wp/Result.cs b/src/wp/BarcodeScannerResult.cs similarity index 100% rename from src/wp/Result.cs rename to src/wp/BarcodeScannerResult.cs diff --git a/src/wp/Scan.xaml b/src/wp/Scan.xaml index 359d34f..af9f5ef 100644 --- a/src/wp/Scan.xaml +++ b/src/wp/Scan.xaml @@ -35,11 +35,6 @@ - - - - - \ No newline at end of file diff --git a/src/wp/Scan.xaml.cs b/src/wp/Scan.xaml.cs index 5c88760..d9bc14d 100644 --- a/src/wp/Scan.xaml.cs +++ b/src/wp/Scan.xaml.cs @@ -16,17 +16,21 @@ namespace BarcodeScanner { public partial class Scan : PhoneApplicationPage { - private PhotoCamera _phoneCamera; - private IBarcodeReader _barcodeReader; - private DispatcherTimer _scanTimer; - private WriteableBitmap _previewBuffer; + private PhotoCamera phoneCamera; + private IBarcodeReader barcodeReader; + private DispatcherTimer scanTimer; + private WriteableBitmap previewBuffer; + + private string scannedCode = string.Empty; + + private bool scanSucceeded; public Scan() { InitializeComponent(); } - public Cordova.Extension.Commands.BarcodeScanner BarcodePlugin + public Cordova.Extension.Commands.BarcodeScanner BarcodeScannerPlugin { get; set; @@ -35,52 +39,55 @@ namespace BarcodeScanner protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { // Initialize the camera object - _phoneCamera = new PhotoCamera(); - _phoneCamera.Initialized += cam_Initialized; + phoneCamera = new PhotoCamera(); + phoneCamera.Initialized += OnCameraInitialized; - CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed; + CameraButtons.ShutterKeyHalfPressed += OnCameraButtonShutterKeyHalfPressed; //Display the camera feed in the UI - viewfinderBrush.SetSource(_phoneCamera); + 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(); + scanTimer = new DispatcherTimer(); + scanTimer.Interval = TimeSpan.FromMilliseconds(250); + scanTimer.Tick += (o, arg) => ScanForBarcode(); base.OnNavigatedTo(e); } - void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e) + private void OnCameraButtonShutterKeyHalfPressed(object sender, EventArgs e) { - _phoneCamera.Focus(); + 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(); + scanTimer.Stop(); - if (_phoneCamera != null) + if (phoneCamera != null) { - // Cleanup - _phoneCamera.Dispose(); - _phoneCamera.Initialized -= cam_Initialized; - CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed; + phoneCamera.Dispose(); + phoneCamera.Initialized -= OnCameraInitialized; + CameraButtons.ShutterKeyHalfPressed -= OnCameraButtonShutterKeyHalfPressed; + } + + if (!scanSucceeded) + { + BarcodeScannerPlugin.OnScanFailed("Cancelled by user"); } } - void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e) + void OnCameraInitialized(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); + phoneCamera.FlashMode = FlashMode.Off; + previewBuffer = new WriteableBitmap((int)phoneCamera.PreviewResolution.Width, (int)phoneCamera.PreviewResolution.Height); - _barcodeReader = new BarcodeReader(); + 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, @@ -91,46 +98,54 @@ namespace BarcodeScanner //supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX); //_bcReader.PossibleFormats = supportedBarcodeFormats; - _barcodeReader.Options.TryHarder = true; + barcodeReader.Options.TryHarder = true; - _barcodeReader.ResultFound += _bcReader_ResultFound; - _scanTimer.Start(); + barcodeReader.ResultFound += OnBarcodeResultFound; + scanTimer.Start(); }); } else { Dispatcher.BeginInvoke(() => { - MessageBox.Show("Unable to initialize the camera"); + BarcodeScannerPlugin.OnScanFailed("Unable to initialize the camera"); }); } } - void _bcReader_ResultFound(Result obj) + private void OnBarcodeResultFound(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)) + if (BarcodeIsValid(obj.Text)) { var barcodeScannerResult = new BarcodeScannerResult(); barcodeScannerResult.format = obj.BarcodeFormat.ToString(); barcodeScannerResult.text = obj.Text; + scanSucceeded = true; - BarcodePlugin.ResultReceived(barcodeScannerResult); + BarcodeScannerPlugin.OnScanSucceeded(barcodeScannerResult); NavigationService.GoBack(); } } + private bool BarcodeIsValid(string barcode) + { + if (barcode.Equals(scannedCode)) + { + return false; + } + + return true; + } + private void ScanForBarcode() { //grab a camera snapshot - _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels); - _previewBuffer.Invalidate(); + 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); - + barcodeReader.Decode(previewBuffer); } - } } \ No newline at end of file From 91d7e9aa4a745fb7a54d1d6649560b0ce255fe7f Mon Sep 17 00:00:00 2001 From: Jakob Gillich Date: Wed, 30 Oct 2013 16:53:40 +0100 Subject: [PATCH 4/5] window.plugins is not defined anymore, replaced it with cordova.plugins Reproduced in 3.1, see #60 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5bf55ae..a43f20b 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ A full example could be: ``` ## Encoding a Barcode ## -The plugin creates the object `window.plugins.barcodeScanner` with the method `encode(type, data, success, fail)`. +The plugin creates the object `cordova.plugins.barcodeScanner` with the method `encode(type, data, success, fail)`. Supported encoding types: * TEXT_TYPE From 4ed50488b6bb98cae1be84a1d03627de70507011 Mon Sep 17 00:00:00 2001 From: Ryan Willoughby Date: Wed, 27 Nov 2013 15:07:49 -0800 Subject: [PATCH 5/5] [1.2.0] upped version --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 7b1e774..8a38ed6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.2.0"> BarcodeScanner Scans Barcodes.