mirror of
https://github.com/wahyd4/BarcodeScanner.git
synced 2026-08-09 04:55:57 +10:00
added windows phone version
This commit is contained in:
+28
-1
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="com.phonegap.plugins.barcodescanner"
|
||||
version="1.0.2">
|
||||
version="1.1.0">
|
||||
|
||||
<name>BarcodeScanner</name>
|
||||
<description>Scans Barcodes.</description>
|
||||
@@ -37,7 +37,34 @@
|
||||
<framework src="AVFoundation.framework" />
|
||||
<framework src="AssetsLibrary.framework" />
|
||||
<framework src="CoreVideo.framework" />
|
||||
<framework src="QuartzCore.framework" />
|
||||
</platform>
|
||||
|
||||
<platform name="wp7">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="BarcodeScanner">
|
||||
<param name="wp-package" value="BarcodeScanner"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<source-file src="src/wp/BarcodeScanner.cs" />
|
||||
<source-file src="src/wp/Result.cs" />
|
||||
<source-file src="src/wp/Scan.xaml.cs" />
|
||||
<source-file src="src/wp/Scan.xaml" />
|
||||
</platform>
|
||||
|
||||
<platform name="wp8">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="BarcodeScanner">
|
||||
<param name="wp-package" value="BarcodeScanner"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<source-file src="src/wp/BarcodeScanner.cs" />
|
||||
<source-file src="src/wp/Result.cs" />
|
||||
<source-file src="src/wp/Scan.xaml.cs" />
|
||||
<source-file src="src/wp/Scan.xaml" />
|
||||
</platform>
|
||||
|
||||
<!-- android -->
|
||||
<platform name="android">
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<phone:PhoneApplicationPage
|
||||
x:Class="BarcodeScanner.Scan"
|
||||
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">
|
||||
|
||||
<!--LayoutRoot is the root grid where all page content is placed-->
|
||||
<Grid x:Name="LayoutRoot" Background="Transparent">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="100" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Canvas x:Name="viewfinderCanvas">
|
||||
|
||||
<!--Camera viewfinder -->
|
||||
<Canvas.Background>
|
||||
<VideoBrush x:Name="viewfinderBrush">
|
||||
<VideoBrush.RelativeTransform>
|
||||
<CompositeTransform
|
||||
x:Name="viewfinderTransform"
|
||||
CenterX="0.5"
|
||||
CenterY="0.5"
|
||||
Rotation="90"/>
|
||||
</VideoBrush.RelativeTransform>
|
||||
</VideoBrush>
|
||||
</Canvas.Background>
|
||||
</Canvas>
|
||||
<!--Used for debugging >-->
|
||||
<StackPanel Grid.Row="1" Margin="20, 0">
|
||||
<TextBlock x:Name="tbBarcodeType" FontWeight="ExtraBold" />
|
||||
<TextBlock x:Name="tbBarcodeData" FontWeight="ExtraBold" TextWrapping="Wrap" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
</phone:PhoneApplicationPage>
|
||||
@@ -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<BarcodeFormat>();
|
||||
//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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user