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

Commit

Permalink
Updated Plugin, refactored some library parts, added description to r…
Browse files Browse the repository at this point in the history
…ead me
  • Loading branch information
huserben authored and EddyVerbruggen committed Jun 26, 2014
1 parent 039d1d0 commit 719f19d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 55 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
BarcodeScanner
==============

Added version that runs with windows mobile devices.
To run on those devices, the zxing.net library must be added to the project (Install as nuget packet - more information on http://www.nuget.org/packages/ZXing.Net)

Windows Phone currently only supports Scaning for codes, feel free to extend it that it can also encode.

Cross-platform BarcodeScanner for Cordova / PhoneGap.

Follows the [Cordova Plugin spec](https://github.com/apache/cordova-plugman/blob/master/plugin_spec.md), so that it works with [Plugman](https://github.com/apache/cordova-plugman).
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</config-file>

<source-file src="src/wp/BarcodeScanner.cs" />
<source-file src="src/wp/Result.cs" />
<source-file src="src/wp/BarcodeScannerResult.cs" />
<source-file src="src/wp/Scan.xaml.cs" />
<source-file src="src/wp/Scan.xaml" />
</platform>
Expand All @@ -59,7 +59,7 @@
</config-file>

<source-file src="src/wp/BarcodeScanner.cs" />
<source-file src="src/wp/Result.cs" />
<source-file src="src/wp/BarcodeScannerResult.cs" />
<source-file src="src/wp/Scan.xaml.cs" />
<source-file src="src/wp/Scan.xaml" />
</platform>
Expand Down
20 changes: 10 additions & 10 deletions src/wp/BarcodeScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ namespace Cordova.Extension.Commands
{
public class BarcodeScanner : BaseCommand
{
PhoneApplicationFrame currentRootVisual;
private PhoneApplicationFrame currentRootVisual;

public void scan(string options)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
currentRootVisual = Application.Current.RootVisual as PhoneApplicationFrame;
currentRootVisual.Navigated += frame_Navigated;
currentRootVisual.Navigated += OnFrameNavigated;
currentRootVisual.Navigate(new Uri("/Plugins/com.phonegap.plugins.barcodescanner/Scan.xaml", UriKind.Relative));
});
}

void frame_Navigated(object sender, NavigationEventArgs e)
private void OnFrameNavigated(object sender, NavigationEventArgs e)
{
var scanPage = e.Content as Scan;
if (scanPage != null)
{
scanPage.BarcodePlugin = this;
scanPage.BarcodeScannerPlugin = this;
}
}

internal void ResultReceived(BarcodeScannerResult scanResult)
public void OnScanFailed(string error)
{
var resultString = JsonHelper.Serialize(scanResult);

DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultString));
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, error));
}

internal void ScanFailed(string error)
public void OnScanSucceeded(BarcodeScannerResult scanResult)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, error));
var resultString = JsonHelper.Serialize(scanResult);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultString));
}
}
}
File renamed without changes.
5 changes: 0 additions & 5 deletions src/wp/Scan.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
</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>
91 changes: 53 additions & 38 deletions src/wp/Scan.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ namespace BarcodeScanner
{
public partial class Scan : PhoneApplicationPage
{
private PhotoCamera _phoneCamera;
private IBarcodeReader _barcodeReader;
private DispatcherTimer _scanTimer;
private WriteableBitmap _previewBuffer;
private PhotoCamera phoneCamera;
private IBarcodeReader barcodeReader;
private DispatcherTimer scanTimer;
private WriteableBitmap previewBuffer;

private string scannedCode = string.Empty;

private bool scanSucceeded;

public Scan()
{
InitializeComponent();
}

public Cordova.Extension.Commands.BarcodeScanner BarcodePlugin
public Cordova.Extension.Commands.BarcodeScanner BarcodeScannerPlugin
{
get;
set;
Expand All @@ -35,52 +39,55 @@ public Cordova.Extension.Commands.BarcodeScanner BarcodePlugin
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Initialize the camera object
_phoneCamera = new PhotoCamera();
_phoneCamera.Initialized += cam_Initialized;
phoneCamera = new PhotoCamera();
phoneCamera.Initialized += OnCameraInitialized;

CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;
CameraButtons.ShutterKeyHalfPressed += OnCameraButtonShutterKeyHalfPressed;

//Display the camera feed in the UI
viewfinderBrush.SetSource(_phoneCamera);
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();
scanTimer = new DispatcherTimer();
scanTimer.Interval = TimeSpan.FromMilliseconds(250);
scanTimer.Tick += (o, arg) => ScanForBarcode();

base.OnNavigatedTo(e);
}

void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
private void OnCameraButtonShutterKeyHalfPressed(object sender, EventArgs e)
{
_phoneCamera.Focus();
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();
scanTimer.Stop();

if (_phoneCamera != null)
if (phoneCamera != null)
{
// Cleanup
_phoneCamera.Dispose();
_phoneCamera.Initialized -= cam_Initialized;
CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
phoneCamera.Dispose();
phoneCamera.Initialized -= OnCameraInitialized;
CameraButtons.ShutterKeyHalfPressed -= OnCameraButtonShutterKeyHalfPressed;
}

if (!scanSucceeded)
{
BarcodeScannerPlugin.OnScanFailed("Cancelled by user");
}
}

void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
void OnCameraInitialized(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);
phoneCamera.FlashMode = FlashMode.Off;
previewBuffer = new WriteableBitmap((int)phoneCamera.PreviewResolution.Width, (int)phoneCamera.PreviewResolution.Height);

_barcodeReader = new BarcodeReader();
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,
Expand All @@ -91,46 +98,54 @@ void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEv
//supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX);
//_bcReader.PossibleFormats = supportedBarcodeFormats;

_barcodeReader.Options.TryHarder = true;
barcodeReader.Options.TryHarder = true;

_barcodeReader.ResultFound += _bcReader_ResultFound;
_scanTimer.Start();
barcodeReader.ResultFound += OnBarcodeResultFound;
scanTimer.Start();
});
}
else
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Unable to initialize the camera");
BarcodeScannerPlugin.OnScanFailed("Unable to initialize the camera");
});
}
}

void _bcReader_ResultFound(Result obj)
private void OnBarcodeResultFound(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))
if (BarcodeIsValid(obj.Text))
{
var barcodeScannerResult = new BarcodeScannerResult();
barcodeScannerResult.format = obj.BarcodeFormat.ToString();
barcodeScannerResult.text = obj.Text;
scanSucceeded = true;

BarcodePlugin.ResultReceived(barcodeScannerResult);
BarcodeScannerPlugin.OnScanSucceeded(barcodeScannerResult);
NavigationService.GoBack();
}
}

private bool BarcodeIsValid(string barcode)
{
if (barcode.Equals(scannedCode))
{
return false;
}

return true;
}

private void ScanForBarcode()
{
//grab a camera snapshot
_phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
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);

barcodeReader.Decode(previewBuffer);
}

}
}

0 comments on commit 719f19d

Please sign in to comment.