Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Source/ZXing.Net.Mobile.Android/CameraAccess/CameraAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CameraAnalyzer
private DateTime _lastPreviewAnalysis = DateTime.UtcNow;
private bool _wasScanned;
IScannerSessionHost _scannerHost;
private bool _cameraSetup;

public CameraAnalyzer(SurfaceView surfaceView, IScannerSessionHost scannerHost)
{
Expand Down Expand Up @@ -41,15 +42,23 @@ public void ResumeAnalysis()

public void ShutdownCamera()
{
IsAnalyzing = false;
_cameraEventListener.OnPreviewFrameReady -= HandleOnPreviewFrameReady;
_cameraController.ShutdownCamera();
if (_cameraSetup)
{
IsAnalyzing = false;
_cameraEventListener.OnPreviewFrameReady -= HandleOnPreviewFrameReady;
_cameraController.ShutdownCamera();
_cameraSetup = false;
}
}

public void SetupCamera()
{
_cameraEventListener.OnPreviewFrameReady += HandleOnPreviewFrameReady;
_cameraController.SetupCamera();
if (!_cameraSetup)
{
_cameraEventListener.OnPreviewFrameReady += HandleOnPreviewFrameReady;
_cameraController.SetupCamera();
_cameraSetup = true;
}
}

public void AutoFocus()
Expand All @@ -64,7 +73,9 @@ public void AutoFocus(int x, int y)

public void RefreshCamera()
{
_cameraController.RefreshCamera();
//only refresh the camera if it is actually setup
if(_cameraSetup)
_cameraController.RefreshCamera();
}

private bool CanAnalyzeFrame
Expand Down
20 changes: 15 additions & 5 deletions Source/ZXing.Net.Mobile.Android/ZXingSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ private void Init()

public async void SurfaceCreated(ISurfaceHolder holder)
{
await ZXing.Net.Mobile.Android.PermissionsHandler.PermissionRequestTask;

_cameraAnalyzer.SetupCamera();

_surfaceCreated = true;
//avoid duplicate setups, forcing the camera to be setup even when the camera was not scanning (this can happen when resuming the app)
if (!_surfaceCreated)
{
await ZXing.Net.Mobile.Android.PermissionsHandler.PermissionRequestTask;
_cameraAnalyzer.SetupCamera();
_surfaceCreated = true;
}
}

public async void SurfaceChanged(ISurfaceHolder holder, Format format, int wx, int hx)
Expand Down Expand Up @@ -98,6 +100,12 @@ public void AutoFocus(int x, int y)

public void StartScanning(Action<Result> scanResultCallback, MobileBarcodeScanningOptions options = null)
{
//fix Android 7 bug: camera freezes because surfacedestroyed function isn't always called correct, the old surfaceview was still visible.
this.Visibility = ViewStates.Visible;

//make sure the camera is setup
_cameraAnalyzer.SetupCamera();

ScanningOptions = options ?? MobileBarcodeScanningOptions.Default;

_cameraAnalyzer.BarcodeFound += (sender, result) =>
Expand All @@ -110,6 +118,8 @@ public void StartScanning(Action<Result> scanResultCallback, MobileBarcodeScanni
public void StopScanning()
{
_cameraAnalyzer.ShutdownCamera();
//fix Android 7 bug: camera freezes because surfacedestroyed function isn't always called correct, the old surfaceview was still visible.
this.Visibility = ViewStates.Gone;
}

public void PauseAnalysis()
Expand Down