Skip to content

Commit

Permalink
Added some more null checks and error catching around cleaning up of …
Browse files Browse the repository at this point in the history
…the camera for Windows Phone. This should fix Redth#82
  • Loading branch information
Redth committed Jan 28, 2014
1 parent d10fcb0 commit ce69826
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ZXing.Net.Mobile/WindowsPhone/ScanPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e)

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
scannerControl.StopScanning();
try { scannerControl.StopScanning(); }
catch (Exception ex) { }

base.OnNavigatingFrom(e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/ZXing.Net.Mobile/WindowsPhone/SimpleCameraReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public void Stop()

if (_photoCamera != null)
{
_photoCamera.Initialized -= OnPhotoCameraInitialized;
_photoCamera.Dispose();
_photoCamera = null;
}
Expand All @@ -222,6 +223,9 @@ public void ShutterHalfPressed()

private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
{
if (_photoCamera == null)
return;

var width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
var height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

Expand Down
17 changes: 12 additions & 5 deletions src/ZXing.Net.Mobile/WindowsPhone/ZXingScannerControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,20 @@ public void Cancel()
private void ReaderOnCameraInitialized(object sender, bool initialized)
{
// We dispatch (invoke) to avoid access exceptions
Dispatcher.BeginInvoke(() => _previewTransform.Rotation = _reader.CameraOrientation);
Dispatcher.BeginInvoke(() =>
{
if (_reader != null && _previewTransform != null)
_previewTransform.Rotation = _reader.CameraOrientation;
});

// We can set if Camera should flash or not when focused
_reader.FlashMode = FlashMode.Off;
if (_reader != null)
{
// We can set if Camera should flash or not when focused
_reader.FlashMode = FlashMode.Off;

// Starts the capturing process
_reader.Start();
// Starts the capturing process
_reader.Start();
}
}

private void DisplayResult(Result result)
Expand Down

0 comments on commit ce69826

Please sign in to comment.