/* * Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ namespace WPCordovaClassLib.Cordova.Commands { using System.Runtime.Serialization; using Microsoft.Phone.Tasks; using WPCordovaClassLib.Cordova.JSON; using ZXing; /// /// Class that extends cordova with Barcode scanner functionality. /// public class BarcodeScanner : BaseCommand { /// /// Scans the barcode. /// /// Parameter is ignored. public void scan(string options) { var task = new BarcodeScannerTask(); task.Completed += this.TaskCompleted; task.Show(); } /// /// Handler for barcode scanner task. /// /// The sender. /// The scan result. private void TaskCompleted(object sender, BarcodeScannerTask.ScanResult e) { PluginResult result; switch (e.TaskResult) { case TaskResult.OK: result = new PluginResult(PluginResult.Status.OK); result.Message = JsonHelper.Serialize(new BarcodeResult(e.Barcode)); break; case TaskResult.Cancel: // If scan is cancelled we return PluginResult.Status.OK with Message contains cancelled: true // See plugin docs https://github.com/MSOpenTech/BarcodeScanner#using-the-plugin result = new PluginResult(PluginResult.Status.OK); result.Message = JsonHelper.Serialize(new BarcodeResult()); break; default: result = new PluginResult(PluginResult.Status.ERROR); break; } this.DispatchCommandResult(result); } } /// /// Represents the barcode scan result, that should be serialized and passed to JS layer. /// [DataContract] public sealed class BarcodeResult { /// /// Initializes a new instance of the class. /// /// if set to true [canceled]. public BarcodeResult(bool canceled = true) { this.Cancelled = canceled; } /// /// Initializes a new instance of the class. /// /// The barcode result. public BarcodeResult(Result barcode) { this.Cancelled = false; this.Format = barcode.BarcodeFormat.ToString(); this.Text = barcode.Text; } /// /// Gets a value indicating whether barcode scan is cancelled. /// /// /// true if cancelled; otherwise, false. /// [DataMember(Name = "cancelled")] public bool Cancelled { get; private set; } /// /// Gets the format of barcode. /// /// /// The barcode format. /// [DataMember(Name = "format")] public string Format { get; private set; } /// /// Gets the barcode text. /// /// /// The barcode text. /// [DataMember(Name = "text")] public string Text { get; private set; } } }