Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Adds support for Windows phone 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Kotikov authored and Ryan Willoughby committed Jul 31, 2014
1 parent 74f78db commit b94b4f6
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 2 deletions.
24 changes: 22 additions & 2 deletions plugin.xml
Expand Up @@ -15,8 +15,6 @@
<clobbers target="cordova.plugins.barcodeScanner" />
</js-module>

<asset src="www/barcodescanner.js" target="barcodescanner.js" />

<!-- ios -->
<platform name="ios">
<!-- Cordova >= 2.8 -->
Expand Down Expand Up @@ -295,4 +293,26 @@
<framework src="src/windows8/lib/ZXing.winmd" custom="true" />
<framework src="src/windows8/lib/WinRTBarcodeReader.winmd" custom="true" />
</platform>

<!-- Windows Phone 8 -->
<platform name="wp8">

<config-file target="config.xml" parent="/*">
<feature name="BarcodeScanner">
<param name="wp-package" value="BarcodeScanner"/>
</feature>
</config-file>

<config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Capabilities">
<Capability Name="ID_CAP_ISV_CAMERA" />
</config-file>

<framework src="src/wp8/lib/zxing.wp8.0.dll" custom="true" />

<source-file src="src/wp8/BarcodeScanner.cs" />
<source-file src="src/wp8/BarcodeScannerTask.cs" />
<source-file src="src/wp8/BarcodeScannerUI.xaml" />
<source-file src="src/wp8/BarcodeScannerUI.xaml.cs" />

</platform>
</plugin>
117 changes: 117 additions & 0 deletions src/wp8/BarcodeScanner.cs
@@ -0,0 +1,117 @@
/*
* 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;

/// <summary>
/// Class that extends cordova with Barcode scanner functionality.
/// </summary>
public class BarcodeScanner : BaseCommand
{
/// <summary>
/// Scans the barcode.
/// </summary>
/// <param name="options">Parameter is ignored.</param>
public void scan(string options)
{
var task = new BarcodeScannerTask();
task.Completed += this.TaskCompleted;
task.Show();
}

/// <summary>
/// Handler for barcode scanner task.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The scan result.</param>
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);
}
}

/// <summary>
/// Represents the barcode scan result, that should be serialized and passed to JS layer.
/// </summary>
[DataContract]
public sealed class BarcodeResult
{
/// <summary>
/// Initializes a new instance of the <see cref="BarcodeResult"/> class.
/// </summary>
/// <param name="canceled">if set to <c>true</c> [canceled].</param>
public BarcodeResult(bool canceled = true)
{
this.Cancelled = canceled;
}

/// <summary>
/// Initializes a new instance of the <see cref="BarcodeResult"/> class.
/// </summary>
/// <param name="barcode">The barcode result.</param>
public BarcodeResult(Result barcode)
{
this.Cancelled = false;
this.Format = (int)barcode.BarcodeFormat;
this.Text = barcode.Text;
}

/// <summary>
/// Gets a value indicating whether barcode scan is cancelled.
/// </summary>
/// <value>
/// <c>true</c> if cancelled; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "cancelled")]
public bool Cancelled { get; private set; }

/// <summary>
/// Gets the format of barcode.
/// </summary>
/// <value>
/// The barcode format.
/// </value>
[DataMember(Name = "format")]
public int Format { get; private set; }

/// <summary>
/// Gets the barcode text.
/// </summary>
/// <value>
/// The barcode text.
/// </value>
[DataMember(Name = "text")]
public string Text { get; private set; }
}
}
103 changes: 103 additions & 0 deletions src/wp8/BarcodeScannerTask.cs
@@ -0,0 +1,103 @@
/*
* 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;
using System.Windows;
using System.Windows.Navigation;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using ZXing;

/// <summary>
/// Class that represents barcode scanner task that mimics standart WP8 tasks.
/// </summary>
public class BarcodeScannerTask
{
/// <summary>
/// Occurs when task is [completed].
/// </summary>
public event EventHandler<ScanResult> Completed;

/// <summary>
/// Shows barcode scanner interface.
/// </summary>
public void Show()
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var root = Application.Current.RootVisual as PhoneApplicationFrame;
if (root == null)
{
return;
}
root.Navigated += this.OnNavigated;
root.Navigate(new Uri("/Plugins/com.phonegap.plugins.barcodescanner/BarcodeScannerUI.xaml", UriKind.Relative));
});
}

/// <summary>
/// Called when [navigated].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="NavigationEventArgs"/> instance containing the event data.</param>
private void OnNavigated(object sender, NavigationEventArgs e)
{
if (!(e.Content is BarcodeScannerUI))
{
return;
}

var phoneApplicationFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (phoneApplicationFrame != null)
{
phoneApplicationFrame.Navigated -= this.OnNavigated;
}

var barcodeScanner = (BarcodeScannerUI)e.Content;

if (barcodeScanner != null)
{
barcodeScanner.Completed += this.Completed;
}
else if (this.Completed != null)
{
this.Completed(this, new ScanResult(TaskResult.Cancel));
}
}

/// <summary>
/// Represents barcode scan result.
/// </summary>
public class ScanResult : TaskEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="ScanResult"/> class.
/// </summary>
/// <param name="taskResult">One of the enumeration values that specifies the status of the task.</param>
public ScanResult(TaskResult taskResult)
: base(taskResult)
{
}

/// <summary>
/// Gets the barcode scan result.
/// </summary>
/// <value>
/// The barcode scan result.
/// </value>
public Result Barcode { get; internal set; }
}
}
}
40 changes: 40 additions & 0 deletions src/wp8/BarcodeScannerUI.xaml
@@ -0,0 +1,40 @@
<!--
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.
-->
<phone:PhoneApplicationPage
x:Class="WPCordovaClassLib.Cordova.Commands.BarcodeScannerUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<Grid Background="Transparent">
<Canvas x:Name="CameraCanvas">
<Canvas.Background>
<VideoBrush x:Name="CameraBrush">
<VideoBrush.RelativeTransform>
<CompositeTransform
CenterX="0.5"
CenterY="0.5"
Rotation="90"/>
</VideoBrush.RelativeTransform>
</VideoBrush>
</Canvas.Background>
</Canvas>
</Grid>

</phone:PhoneApplicationPage>

0 comments on commit b94b4f6

Please sign in to comment.