From 4bf8e00c552a382b3a158ef176f5c1eadda785ae Mon Sep 17 00:00:00 2001 From: Ota Mares Date: Wed, 18 Mar 2015 13:51:37 +0100 Subject: [PATCH 1/2] Add Preview, Camera, Decoder events and bindings --- .../BarcodeScannerRenderer.cs | 37 ++++++ .../Camera/CameraConfigurator.cs | 44 +++++-- .../Camera/PreviewFrameCallback.cs | 2 +- .../Decoder/BarcodeDecoder.cs | 4 + Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs | 2 +- Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs | 11 ++ Rb.Forms.Barcode.Pcl/BarcodeScanner.cs | 121 ++++++++++++++++-- .../Extensions/EventExtension.cs | 23 ++++ .../Rb.Forms.Barcode.Pcl.csproj | 3 + Sample/Sample.Pcl/Pages/ScannerPage.cs | 34 ++++- 10 files changed, 256 insertions(+), 25 deletions(-) create mode 100644 Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs create mode 100644 Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs diff --git a/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs b/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs index 59fbc9e..2a80912 100644 --- a/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs +++ b/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs @@ -5,6 +5,7 @@ using Rb.Forms.Barcode.Pcl; using Rb.Forms.Barcode.Pcl.Logger; +using Rb.Forms.Barcode.Pcl.Extensions; using Rb.Forms.Barcode.Droid; using Rb.Forms.Barcode.Droid.Camera; using Rb.Forms.Barcode.Droid.Decoder; @@ -101,6 +102,32 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE releaseCamera(); } } + + if (e.PropertyName == BarcodeScanner.PreviewActiveProperty.PropertyName) + { + this.Debug("ScannerActive [{0}]", Element.PreviewActive); + + if (Element.PreviewActive) { + startPreview(); + } + + if (!Element.PreviewActive) { + haltPreview(); + } + } + + if (e.PropertyName == BarcodeScanner.BarcodeDecoderProperty.PropertyName) + { + this.Debug("Decoder state [{0}]", Element.BarcodeDecoder); + + if (Element.BarcodeDecoder) { + barcodeDecoder.RefreshToken(); + } + + if (!Element.BarcodeDecoder) { + barcodeDecoder.CancelDecoding(); + } + } } protected override void OnVisibilityChanged(global::Android.Views.View view, ViewStates visibility) @@ -145,10 +172,13 @@ private void openCamera() } barcodeDecoder.RefreshToken(); + Element.BarcodeDecoder = true; try { cameraControl.OpenCamera(); cameraControl.AssignPreview(Control.Holder); + + Element.OnCameraOpened(); } catch (Exception ex) { this.Debug("Unable to open camera"); this.Debug(ex.ToString()); @@ -159,10 +189,13 @@ private void releaseCamera() { try { barcodeDecoder.CancelDecoding(); + Element.BarcodeDecoder = false; + cameraControl.ReleaseCamera(); autoFocus.Enabled = false; BarcodeScannerRenderer.reuseCamera = false; + Element.OnCameraReleased(); } catch (Exception ex) { this.Debug("Unable to release camera"); this.Debug(ex.ToString()); @@ -184,12 +217,16 @@ private void startPreview() if (cameraControl.AutoFocusMode) { cameraControl.AutoFocus(autoFocus); } + + Element.PreviewActive = true; } private void haltPreview() { autoFocus.Enabled = false; cameraControl.HaltPreview(); + + Element.PreviewActive = false; } } } diff --git a/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs b/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs index 25d7310..b9e182b 100644 --- a/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs +++ b/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs @@ -20,23 +20,36 @@ public AndroidCamera Configure(AndroidCamera camera) this.Debug("Focus Mode [{0}]", focusMode); parameters.FocusMode = focusMode; - var sceneMode = determineSceneMode(camera); - this.Debug("Scene Mode [{0}]", sceneMode); - parameters.SceneMode = sceneMode; - this.Debug("Metering area [{0}]", (parameters.MaxNumMeteringAreas > 0).ToString()); - if (parameters.MaxNumMeteringAreas > 0) { - parameters.MeteringAreas = createAreas(); + if (isPickyDevice()) { + this.Debug("Used device is marked as picky. Skipping detailed configuration to ensure function compatibility."); } - this.Debug("Video stabilization [{0}]", parameters.IsVideoStabilizationSupported.ToString()); - if (parameters.IsVideoStabilizationSupported) { - parameters.VideoStabilization = true; - } + if (!isPickyDevice()) { + var sceneMode = determineSceneMode(camera); + this.Debug("Scene Mode [{0}]", sceneMode); + parameters.SceneMode = sceneMode; + + this.Debug("Metering area [{0}]", (parameters.MaxNumMeteringAreas > 0).ToString()); + if (parameters.MaxNumMeteringAreas > 0) { + parameters.MeteringAreas = createAreas(); + } + + this.Debug("Focusing area [{0}]", (parameters.MaxNumFocusAreas > 0).ToString()); + if (parameters.MaxNumFocusAreas > 0) { + parameters.FocusAreas = createAreas(); + } - var whiteBalance = determineWhiteBalance(camera); - this.Debug("White balance [{0}]", whiteBalance); - parameters.WhiteBalance = whiteBalance; + this.Debug("Video stabilization [{0}]", parameters.IsVideoStabilizationSupported.ToString()); + if (parameters.IsVideoStabilizationSupported) { + parameters.VideoStabilization = true; + } + + var whiteBalance = determineWhiteBalance(camera); + this.Debug("White balance [{0}]", whiteBalance); + parameters.WhiteBalance = whiteBalance; + + } camera.SetParameters(parameters); @@ -92,6 +105,11 @@ private string determineWhiteBalance(AndroidCamera camera) area }; } + + private bool isPickyDevice() + { + return Android.OS.Build.Manufacturer.Contains("samsung"); + } } } #pragma warning restore 618 \ No newline at end of file diff --git a/Rb.Forms.Barcode.Droid/Camera/PreviewFrameCallback.cs b/Rb.Forms.Barcode.Droid/Camera/PreviewFrameCallback.cs index a2453db..cd9c855 100644 --- a/Rb.Forms.Barcode.Droid/Camera/PreviewFrameCallback.cs +++ b/Rb.Forms.Barcode.Droid/Camera/PreviewFrameCallback.cs @@ -32,7 +32,7 @@ async public void OnPreviewFrame(byte[] bytes, AndroidCamera camera) var barcode = await decoder; if (!string.IsNullOrWhiteSpace(barcode)) { - scanner.RaiseBarcodeFoundEvent(barcode); + scanner.Barcode = barcode; } } } diff --git a/Rb.Forms.Barcode.Droid/Decoder/BarcodeDecoder.cs b/Rb.Forms.Barcode.Droid/Decoder/BarcodeDecoder.cs index 6ccc608..3dfd5be 100644 --- a/Rb.Forms.Barcode.Droid/Decoder/BarcodeDecoder.cs +++ b/Rb.Forms.Barcode.Droid/Decoder/BarcodeDecoder.cs @@ -34,6 +34,10 @@ public Task DecodeAsync(byte[] bytes, int width, int height) return null; } + if (cancellationTokenSource.IsCancellationRequested) { + return null; + } + lastPreviewScanAnalysis = DateTime.UtcNow; currentTask = Task.Run(() => { diff --git a/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs b/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs index 3b877ae..32e6b37 100644 --- a/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs +++ b/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs @@ -2,7 +2,7 @@ namespace Rb.Forms.Barcode.Pcl { - public sealed class BarcodeFoundEventArgs : EventArgs + public class BarcodeFoundEventArgs : EventArgs { public string Barcode { get; private set; } diff --git a/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs b/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs new file mode 100644 index 0000000..220adde --- /dev/null +++ b/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs @@ -0,0 +1,11 @@ +using System; + +namespace Rb.Forms.Barcode.Pcl +{ + public class BarcodeReadEventArgs : BarcodeFoundEventArgs + { + public BarcodeReadEventArgs(string barcode) : base(barcode) + { + } + } +} diff --git a/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs b/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs index d491616..5efa1d3 100644 --- a/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs +++ b/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs @@ -2,33 +2,136 @@ using Xamarin.Forms; using System.Diagnostics; +using Rb.Forms.Barcode.Pcl.Extensions; namespace Rb.Forms.Barcode.Pcl { public class BarcodeScanner : View { + /// + /// OneWay source to target binding for the current barcode. + /// public static readonly BindableProperty BarcodeProperty = BindableProperty.Create( - p => p.Barcode, default(string), BindingMode.OneWayToSource); + p => p.Barcode, default(string), propertyChanged: OnBarcodeChanged + ); + /// + /// Gets or sets the current barcode. + /// + /// The barcode. public String Barcode { get { return (String) GetValue(BarcodeProperty); } - set { SetValue(BarcodeProperty, value); } + set { SetValue(BarcodeProperty, value); OnBarcodeRead(value); } } - public event EventHandler BarcodeFound; + /// + /// TwoWay binding to control the scanner preview and decoder state. + /// If set to true the preview is active, false deactivates the preview image. + /// + public static readonly BindableProperty PreviewActiveProperty = + BindableProperty.Create( + p => p.PreviewActive, default(bool), BindingMode.TwoWay, propertyChanged: OnPreviewActiveChanged + ); - public void RaiseBarcodeFoundEvent(String barcode) + /// + /// Gets or controlls the current preview state. + /// + /// The barcode. + public bool PreviewActive { - Barcode = barcode; + get { return (bool) GetValue(PreviewActiveProperty); } + set { SetValue(PreviewActiveProperty, value); } + } + + /// + /// TwoWay binding for barcode decoder control. + /// If set to true the decoder tries to read the barcode from the surface image, false deactivates the decoder. + /// + public static readonly BindableProperty BarcodeDecoderProperty = + BindableProperty.Create( + p => p.BarcodeDecoder, default(bool), BindingMode.TwoWay + ); + + /// + /// Gets or controlls the decoder state. + /// + /// The barcode. + public bool BarcodeDecoder + { + get { return (bool) GetValue(BarcodeDecoderProperty); } + set { SetValue(BarcodeDecoderProperty, value); } + } + + /// + /// Occurs only when the barcode text changes. + /// + public event EventHandler BarcodeChanged; + + /// + /// Occurs every time when a barcode is decoded from the preview, even if the value is the same as the previews one. + /// + public event EventHandler BarcodeDecoded; + + /// + /// Occurs as soon as the surfaces starts previewing. + /// + public event EventHandler PreviewActivated; - Debug.WriteLine("[ScannerView] RaiseBarcodeFoundEvent [{0}]", barcode, null); - if (null == this.BarcodeFound) { - return; + /// + /// Occurs when the surfaces stops previewing. + /// + public event EventHandler PreviewDeactivated; + + /// + /// Occurs after the camera was opened. + /// + public event EventHandler CameraOpened; + + /// + /// Occurs after the camera was released. + /// + public event EventHandler CameraReleased; + + public void OnCameraOpened() + { + CameraOpened.Raise(this, EventArgs.Empty); + } + + public void OnCameraReleased() + { + CameraReleased.Raise(this, EventArgs.Empty); + } + + private static void OnBarcodeChanged(BindableObject bindable, String oldValue, String newBarcode) + { + Debug.WriteLine("[ScannerView] OnBarcodeChanged [{0}]", newBarcode, null); + var b = (BarcodeScanner) bindable; + + b.BarcodeChanged.Raise(b, new BarcodeFoundEventArgs(newBarcode)); + } + + private void OnBarcodeRead(String barcode) + { + Debug.WriteLine("[ScannerView] OnBarcodeRead [{0}]", barcode, null); + + BarcodeDecoded.Raise(this, new BarcodeReadEventArgs(barcode)); + } + + private static void OnPreviewActiveChanged(BindableObject bindable, bool oldState, bool newState) + { + var b = (BarcodeScanner) bindable; + + Debug.WriteLine("[ScannerView] OnPreviewActiveChanged to [{0}]", newState); + + if (newState) { + b.PreviewActivated.Raise(b, new EventArgs()); } - this.BarcodeFound(this, new BarcodeFoundEventArgs(barcode)); + if (!newState) { + b.PreviewDeactivated.Raise(b, new EventArgs()); + } } } } diff --git a/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs b/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs new file mode 100644 index 0000000..b13867c --- /dev/null +++ b/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs @@ -0,0 +1,23 @@ +using System; + +namespace Rb.Forms.Barcode.Pcl.Extensions +{ + public static class EventExtension + { + public static void Raise(this EventHandler handler, object sender) + { + if (handler != null) { + handler(sender, EventArgs.Empty); + } + } + + public static void Raise(this EventHandler handler, object sender, T args) where T : EventArgs + { + if (handler != null) { + handler(sender, args); + } + + } + } +} + diff --git a/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj b/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj index 1efe815..8859cb7 100644 --- a/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj +++ b/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj @@ -33,6 +33,8 @@ + + @@ -49,5 +51,6 @@ + diff --git a/Sample/Sample.Pcl/Pages/ScannerPage.cs b/Sample/Sample.Pcl/Pages/ScannerPage.cs index 1a8b7e3..e34be38 100644 --- a/Sample/Sample.Pcl/Pages/ScannerPage.cs +++ b/Sample/Sample.Pcl/Pages/ScannerPage.cs @@ -1,6 +1,7 @@ using System; using Rb.Forms.Barcode.Pcl; using Xamarin.Forms; +using System.Diagnostics; namespace Sample.Pcl.Pages { @@ -34,10 +35,20 @@ public ScannerPage() /** * Event that gets executed as soon as a barcode is detected. */ - barcodeScanner.BarcodeFound += (object sender, BarcodeFoundEventArgs e) => { + barcodeScanner.BarcodeChanged += (object sender, BarcodeFoundEventArgs e) => { flashScreenAsync(sender, e); result.Text = String.Format("Last Barcode: {0}", e.Barcode); }; + + barcodeScanner.BarcodeDecoded += (sender, e) => { + Debug.WriteLine("Read barcode [{0}]", e.Barcode); + }; + barcodeScanner.CameraOpened += (sender, e) => { + Debug.WriteLine("ScannerAvailable"); + }; + barcodeScanner.CameraReleased += (sender, e) => { + Debug.WriteLine("ScannerUnavailable"); + }; /** * So that we can release the camera when turning off phone or switching apps. @@ -92,6 +103,27 @@ private View createViewLayout() XAlign = TextAlignment.Center }); + var b = new Button { + Text = "Toggle preview state." + }; + + b.Clicked += (sender, e) => { + barcodeScanner.PreviewActive = !barcodeScanner.PreviewActive; + }; + + stackLayout.Children.Add(b); + + + var br = new Button { + Text = "Toggle barcode decoder." + }; + + br.Clicked += (sender, e) => { + barcodeScanner.BarcodeDecoder = !barcodeScanner.BarcodeDecoder; + }; + + stackLayout.Children.Add(br); + relativeLayout.Children.Add( stackLayout, Constraint.RelativeToParent ((parent) => { return 0; }) From 9c8ded54b5e4b3d03cd011a706e8fde878346ec7 Mon Sep 17 00:00:00 2001 From: Ota Mares Date: Wed, 18 Mar 2015 15:25:38 +0100 Subject: [PATCH 2/2] Added documentation for new events and properties --- README.md | 13 +++- .../BarcodeScannerRenderer.cs | 9 +++ .../Camera/CameraConfigurator.cs | 1 - ...eFoundEventArgs.cs => BarcodeEventArgs.cs} | 4 +- Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs | 11 --- Rb.Forms.Barcode.Pcl/BarcodeScanner.cs | 17 ++-- .../Extensions/EventExtension.cs | 1 - .../Rb.Forms.Barcode.Pcl.csproj | 3 +- Sample/Sample.Pcl/Pages/ScannerPage.cs | 77 +++++++++++-------- 9 files changed, 76 insertions(+), 60 deletions(-) rename Rb.Forms.Barcode.Pcl/{BarcodeFoundEventArgs.cs => BarcodeEventArgs.cs} (62%) delete mode 100644 Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs diff --git a/README.md b/README.md index f54de7b..e9c8fd5 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,16 @@ You can enable or disable the scanner and preview using the `BarcodeScanner.IsEn What | Type | Description ---- | ---- | ----------- -`BarcodeScanner.BarcodeFound` | EventHandler | The event gets raised when a barcode was found and decoded. Use this to start of your custom logic. -`BarcodeScanner.IsEnabled` | Property | If `true` opens the camera and starts the preview. If `false` stops the preview and releases the camera. -`BarcodeScanner.Barcode` | Property | A bindable property that holds the value of the last found barcode. +`BarcodeScanner.BarcodeChanged` | EventHandler | Raised only when the barcode text changes. +`BarcodeScanner.BarcodeDecoded` | EventHandler | Raised every time when a barcode is decoded from the preview, even if the value is the same as the previews one. +`BarcodeScanner.PreviewActivated` | EventHandler | Raised after the preview image gets active. +`BarcodeScanner.PreviewDeactivated` | EventHandler | Raised after the preview image gets deactivated. +`BarcodeScanner.CameraOpened` | EventHandler | Raised after the camera was obtained. +`BarcodeScanner.CameraReleased` | EventHandler | Raised after the camera was released. +`BarcodeScanner.Barcode` | Property | Holds the value of the last found barcode. +`BarcodeScanner.IsEnabled` | Property | If `true` opens the camera and activates the preview. `false` deactivates the preview and releases the camera. +`BarcodeScanner.PreviewActive` | Property | If `true` the preview image gets updated. `false` no preview for you! +`BarcodeScanner.BarcodeDecoder` | Property | If `true` the decoder is active and tries to decode barcodes out of the image. `false` turns the decoder off, the preview is still active but barcodes will not be decoded. ### Debugging diff --git a/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs b/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs index 2a80912..219431a 100644 --- a/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs +++ b/Rb.Forms.Barcode.Droid/BarcodeScannerRenderer.cs @@ -12,6 +12,7 @@ using Rb.Forms.Barcode.Droid.Logger; using Android.Views; +using System.Threading.Tasks; [assembly: ExportRenderer(typeof(BarcodeScanner), typeof(BarcodeScannerRenderer))] namespace Rb.Forms.Barcode.Droid @@ -32,6 +33,10 @@ public static void Init() public void SurfaceCreated(ISurfaceHolder holder) { + if (!Element.IsEnabled) { + return; + } + this.Debug("SurfaceCreated"); if (cameraControl.CameraOpen) { @@ -44,6 +49,10 @@ public void SurfaceCreated(ISurfaceHolder holder) public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height) { + if (!Element.IsEnabled) { + return; + } + this.Debug("SurfaceChanged"); try { diff --git a/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs b/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs index b9e182b..28c809a 100644 --- a/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs +++ b/Rb.Forms.Barcode.Droid/Camera/CameraConfigurator.cs @@ -48,7 +48,6 @@ public AndroidCamera Configure(AndroidCamera camera) var whiteBalance = determineWhiteBalance(camera); this.Debug("White balance [{0}]", whiteBalance); parameters.WhiteBalance = whiteBalance; - } camera.SetParameters(parameters); diff --git a/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs b/Rb.Forms.Barcode.Pcl/BarcodeEventArgs.cs similarity index 62% rename from Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs rename to Rb.Forms.Barcode.Pcl/BarcodeEventArgs.cs index 32e6b37..edbe390 100644 --- a/Rb.Forms.Barcode.Pcl/BarcodeFoundEventArgs.cs +++ b/Rb.Forms.Barcode.Pcl/BarcodeEventArgs.cs @@ -2,11 +2,11 @@ namespace Rb.Forms.Barcode.Pcl { - public class BarcodeFoundEventArgs : EventArgs + public class BarcodeEventArgs : EventArgs { public string Barcode { get; private set; } - public BarcodeFoundEventArgs(string barcode) + public BarcodeEventArgs(string barcode) { Barcode = barcode; } diff --git a/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs b/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs deleted file mode 100644 index 220adde..0000000 --- a/Rb.Forms.Barcode.Pcl/BarcodeReadEventArgs.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Rb.Forms.Barcode.Pcl -{ - public class BarcodeReadEventArgs : BarcodeFoundEventArgs - { - public BarcodeReadEventArgs(string barcode) : base(barcode) - { - } - } -} diff --git a/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs b/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs index 5efa1d3..20ca6c8 100644 --- a/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs +++ b/Rb.Forms.Barcode.Pcl/BarcodeScanner.cs @@ -23,7 +23,10 @@ public class BarcodeScanner : View public String Barcode { get { return (String) GetValue(BarcodeProperty); } - set { SetValue(BarcodeProperty, value); OnBarcodeRead(value); } + set { + SetValue(BarcodeProperty, value); + OnBarcodeDecoded(value); + } } /// @@ -67,12 +70,12 @@ public bool BarcodeDecoder /// /// Occurs only when the barcode text changes. /// - public event EventHandler BarcodeChanged; + public event EventHandler BarcodeChanged; /// /// Occurs every time when a barcode is decoded from the preview, even if the value is the same as the previews one. /// - public event EventHandler BarcodeDecoded; + public event EventHandler BarcodeDecoded; /// /// Occurs as soon as the surfaces starts previewing. @@ -109,14 +112,14 @@ private static void OnBarcodeChanged(BindableObject bindable, String oldValue, S Debug.WriteLine("[ScannerView] OnBarcodeChanged [{0}]", newBarcode, null); var b = (BarcodeScanner) bindable; - b.BarcodeChanged.Raise(b, new BarcodeFoundEventArgs(newBarcode)); + b.BarcodeChanged.Raise(b, new BarcodeEventArgs(newBarcode)); } - private void OnBarcodeRead(String barcode) + private void OnBarcodeDecoded(String barcode) { - Debug.WriteLine("[ScannerView] OnBarcodeRead [{0}]", barcode, null); + Debug.WriteLine("[ScannerView] OnBarcodeDecoded [{0}]", barcode, null); - BarcodeDecoded.Raise(this, new BarcodeReadEventArgs(barcode)); + BarcodeDecoded.Raise(this, new BarcodeEventArgs(barcode)); } private static void OnPreviewActiveChanged(BindableObject bindable, bool oldState, bool newState) diff --git a/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs b/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs index b13867c..63d4cf8 100644 --- a/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs +++ b/Rb.Forms.Barcode.Pcl/Extensions/EventExtension.cs @@ -16,7 +16,6 @@ public static void Raise(this EventHandler handler, object sender, T args) if (handler != null) { handler(sender, args); } - } } } diff --git a/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj b/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj index 8859cb7..60a064e 100644 --- a/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj +++ b/Rb.Forms.Barcode.Pcl/Rb.Forms.Barcode.Pcl.csproj @@ -30,11 +30,10 @@ - - + diff --git a/Sample/Sample.Pcl/Pages/ScannerPage.cs b/Sample/Sample.Pcl/Pages/ScannerPage.cs index e34be38..93bfdd5 100644 --- a/Sample/Sample.Pcl/Pages/ScannerPage.cs +++ b/Sample/Sample.Pcl/Pages/ScannerPage.cs @@ -2,6 +2,8 @@ using Rb.Forms.Barcode.Pcl; using Xamarin.Forms; using System.Diagnostics; +using System.Threading.Tasks; +using System.Threading; namespace Sample.Pcl.Pages { @@ -35,19 +37,13 @@ public ScannerPage() /** * Event that gets executed as soon as a barcode is detected. */ - barcodeScanner.BarcodeChanged += (object sender, BarcodeFoundEventArgs e) => { + barcodeScanner.BarcodeChanged += (object sender, BarcodeEventArgs e) => { flashScreenAsync(sender, e); result.Text = String.Format("Last Barcode: {0}", e.Barcode); }; barcodeScanner.BarcodeDecoded += (sender, e) => { - Debug.WriteLine("Read barcode [{0}]", e.Barcode); - }; - barcodeScanner.CameraOpened += (sender, e) => { - Debug.WriteLine("ScannerAvailable"); - }; - barcodeScanner.CameraReleased += (sender, e) => { - Debug.WriteLine("ScannerUnavailable"); + Debug.WriteLine("Decoded barcode [{0}]", e.Barcode); }; /** @@ -61,9 +57,7 @@ public ScannerPage() private View createViewLayout() { - /** - * Add the scanner itself to the view. - */ + addExpandingViewToRelativeLayout(barcodeScanner); /** @@ -77,8 +71,8 @@ private View createViewLayout() relativeLayout.Children.Add( zeLine, widthConstraint: Constraint.RelativeToParent ((parent) => { return parent.Width - 100; }), - yConstraint: Constraint.RelativeToParent ((parent) => { return parent.Height / 2; }), - xConstraint: Constraint.RelativeToParent ((parent) => { return 50; }) + xConstraint: Constraint.RelativeToParent ((parent) => { return 50; }), + yConstraint: Constraint.RelativeToParent ((parent) => { return parent.Height / 2; }) ); /** @@ -103,32 +97,15 @@ private View createViewLayout() XAlign = TextAlignment.Center }); - var b = new Button { - Text = "Toggle preview state." - }; - - b.Clicked += (sender, e) => { - barcodeScanner.PreviewActive = !barcodeScanner.PreviewActive; - }; - - stackLayout.Children.Add(b); - - - var br = new Button { - Text = "Toggle barcode decoder." - }; - - br.Clicked += (sender, e) => { - barcodeScanner.BarcodeDecoder = !barcodeScanner.BarcodeDecoder; - }; - - stackLayout.Children.Add(br); relativeLayout.Children.Add( stackLayout, Constraint.RelativeToParent ((parent) => { return 0; }) ); + + addControlsToRelativeLayout(); + return relativeLayout; } @@ -155,6 +132,40 @@ private void addExpandingViewToRelativeLayout(View view) ); } + private void addControlsToRelativeLayout() + { + var controls = new StackLayout { + Orientation = StackOrientation.Horizontal + }; + + var preview = new Button { + Text = "Toggle preview", + FontSize = 12 + }; + + var decoder = new Button { + Text = "Toggle decoder", + FontSize = 12 + }; + + preview.Clicked += (sender, e) => { + barcodeScanner.PreviewActive = !barcodeScanner.PreviewActive; + }; + + decoder.Clicked += (sender, e) => { + barcodeScanner.BarcodeDecoder = !barcodeScanner.BarcodeDecoder; + }; + + controls.Children.Add(preview); + controls.Children.Add(decoder); + + relativeLayout.Children.Add( + controls, + widthConstraint: Constraint.RelativeToParent ((parent) => { return parent.Width; }), + yConstraint: Constraint.RelativeToParent ((parent) => { return parent.Height - 60; }) + ); + } + async private void flashScreenAsync(Object sender, EventArgs e) { await flash.FadeTo(1, 150, Easing.CubicIn);