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

Commit

Permalink
Merge pull request #2 from MSOpenTech/2.0-wp8
Browse files Browse the repository at this point in the history
Windows Phone 8.0 improvements and bugfixes:
  • Loading branch information
TimBarham committed Apr 16, 2015
2 parents c74e37a + d0b2b27 commit 47aa588
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@

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

<config-file target="config.xml" parent="/*">
<feature name="BarcodeScanner">
<param name="wp-package" value="BarcodeScanner"/>
Expand All @@ -316,11 +315,12 @@

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

<asset src="src/wp8/assets/cancel.png" target="Images/appbar.cancel.png" />

<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>

<!-- browser -->
Expand Down
8 changes: 7 additions & 1 deletion src/wp8/BarcodeScannerUI.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
shell:SystemTray.IsVisible="True" CacheMode="BitmapCache" >
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="False" Mode="Minimized">
<shell:ApplicationBarIconButton IconUri="/www/Images/appbar.cancel.png" IsEnabled="True" Text="Cancel" Click="CancelScan"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<Grid Background="Transparent">
<Canvas x:Name="CameraCanvas">
Expand All @@ -35,6 +40,7 @@
</VideoBrush>
</Canvas.Background>
</Canvas>
<Rectangle Margin="0" Stroke="Red" Height="2"/>
</Grid>

</phone:PhoneApplicationPage>
71 changes: 55 additions & 16 deletions src/wp8/BarcodeScannerUI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace WPCordovaClassLib.Cordova.Commands
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;

using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

using ZXing;
Expand All @@ -41,6 +43,8 @@ public partial class BarcodeScannerUI
/// </summary>
private PhotoCamera camera;

private DispatcherTimer timer;

/// <summary>
/// Initializes a new instance of the <see cref="BarcodeScannerUI"/> class.
/// This implementation not use camera autofocus.
Expand All @@ -57,6 +61,20 @@ public BarcodeScannerUI()
// Bind events
this.camera.Initialized += this.CameraInitialized;
this.reader.ResultFound += this.ReaderResultFound;

this.timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(100)};
this.timer.Tick += (sender, args) => ScanForBarcode();

this.BackKeyPress += CancelScan;

CameraButtons.ShutterKeyHalfPressed += StartCameraFocus;
camera.AutoFocusCompleted += StartCameraFocus;

}

private void StartCameraFocus(object sender, EventArgs eventArgs)
{
camera.Focus();
}

/// <summary>
Expand Down Expand Up @@ -86,22 +104,13 @@ private void CameraInitialized(object sender, CameraOperationCompletedEventArgs
{
if (e.Succeeded)
{
if (camera.IsFocusSupported)
{
camera.Focus();
}

// Start scan process in separate thread
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
while (result == null)
{
var cameraBuffer = new WriteableBitmap(
(int)camera.PreviewResolution.Width,
(int)camera.PreviewResolution.Height);
camera.GetPreviewBufferArgb32(cameraBuffer.Pixels);
cameraBuffer.Invalidate();
reader.Decode(cameraBuffer);
}
});
this.Dispatcher.BeginInvoke(() => timer.Start());
}
else
{
Expand All @@ -110,6 +119,18 @@ private void CameraInitialized(object sender, CameraOperationCompletedEventArgs
}
}

private void ScanForBarcode()
{
var cameraBuffer = new WriteableBitmap(
(int)camera.PreviewResolution.Width,
(int)camera.PreviewResolution.Height);

camera.GetPreviewBufferArgb32(cameraBuffer.Pixels);
cameraBuffer.Invalidate();

reader.Decode(cameraBuffer);
}

/// <summary>
/// Called when reader find barcode.
/// </summary>
Expand All @@ -126,8 +147,10 @@ private void ReaderResultFound(Result obj)
/// </summary>
private void CleanUp()
{
CameraButtons.ShutterKeyHalfPressed -= StartCameraFocus;
if (this.camera != null)
{
this.camera.AutoFocusCompleted -= StartCameraFocus;
this.camera.Initialized -= this.CameraInitialized;
this.camera.Dispose();
this.camera = null;
Expand All @@ -138,6 +161,22 @@ private void CleanUp()
this.reader.ResultFound -= this.ReaderResultFound;
this.reader = null;
}

if (this.timer != null)
{
this.timer.Stop();
this.timer = null;
}
}

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
NavigationService.GoBack();
}

private void CancelScan(object sender, EventArgs eventArgs)
{
NavigationService.GoBack();
}
}
}
Binary file added src/wp8/assets/cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 47aa588

Please sign in to comment.