Skip to content

Commit

Permalink
Apply @Eskissimo suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon2G committed May 24, 2024
1 parent 12f703e commit b7f8803
Showing 1 changed file with 116 additions and 102 deletions.
218 changes: 116 additions & 102 deletions ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,110 +26,124 @@
using Android.Hardware;
using static Android.Graphics.Paint;
using AndroidX.Camera.Camera2.InterOp;
using Microsoft.Maui.Devices;

namespace ZXing.Net.Maui
{
internal partial class CameraManager
{
AndroidX.Camera.Core.Preview cameraPreview;
ImageAnalysis imageAnalyzer;
PreviewView previewView;
IExecutorService cameraExecutor;
CameraSelector cameraSelector = null;
ProcessCameraProvider cameraProvider;
ICamera camera;

public NativePlatformCameraPreviewView CreateNativeView()
{
previewView = new PreviewView(Context.Context);
cameraExecutor = Executors.NewSingleThreadExecutor();

return previewView;
}

public void Connect()
{
var cameraProviderFuture = ProcessCameraProvider.GetInstance(Context.Context);

cameraProviderFuture.AddListener(new Java.Lang.Runnable(() =>
{
// Used to bind the lifecycle of cameras to the lifecycle owner
cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();

// Preview
cameraPreview = new AndroidX.Camera.Core.Preview.Builder().Build();
cameraPreview.SetSurfaceProvider(previewView.SurfaceProvider);

// Frame by frame analyze
imageAnalyzer = new ImageAnalysis.Builder()
.SetDefaultResolution(new Android.Util.Size(640, 480))
internal partial class CameraManager
{
AndroidX.Camera.Core.Preview cameraPreview;
ImageAnalysis imageAnalyzer;
PreviewView previewView;
IExecutorService cameraExecutor;
CameraSelector cameraSelector = null;
ProcessCameraProvider cameraProvider;
ICamera camera;
public CameraManager()
{
DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged;
}
private void DeviceDisplay_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
if (cameraPreview != null) UpdateCamera();
}

public NativePlatformCameraPreviewView CreateNativeView()
{
previewView = new PreviewView(Context.Context);
cameraExecutor = Executors.NewSingleThreadExecutor();

return previewView;
}

public void Connect()
{
var cameraProviderFuture = ProcessCameraProvider.GetInstance(Context.Context);

cameraProviderFuture.AddListener(new Java.Lang.Runnable(() =>
{
// Used to bind the lifecycle of cameras to the lifecycle owner
cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();

// Preview
cameraPreview = new AndroidX.Camera.Core.Preview.Builder()
.SetDefaultResolution(new Android.Util.Size(640, 480))
.SetIsRgba8888SurfaceRequired(true)
.Build();

cameraPreview.SetSurfaceProvider(previewView.SurfaceProvider);

// Frame by frame analyze
imageAnalyzer = new ImageAnalysis.Builder()
.SetDefaultResolution(new Android.Util.Size(640, 480))
.SetOutputImageFormat(ImageAnalysis.OutputImageFormatRgba8888)
.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
.Build();

imageAnalyzer.SetAnalyzer(cameraExecutor, new FrameAnalyzer((buffer, size) =>
FrameReady?.Invoke(this, new CameraFrameBufferEventArgs(new Readers.PixelBufferHolder { Data = buffer, Size = size }))));

UpdateCamera();

}), ContextCompat.GetMainExecutor(Context.Context)); //GetMainExecutor: returns an Executor that runs on the main thread.
}

public void Disconnect()
{ }

public void UpdateCamera()
{
if (cameraProvider != null)
{
// Unbind use cases before rebinding
cameraProvider.UnbindAll();

var cameraLocation = CameraLocation;

// Select back camera as a default, or front camera otherwise
if (cameraLocation == CameraLocation.Rear && cameraProvider.HasCamera(CameraSelector.DefaultBackCamera))
cameraSelector = CameraSelector.DefaultBackCamera;
else if (cameraLocation == CameraLocation.Front && cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera))
cameraSelector = CameraSelector.DefaultFrontCamera;
else
cameraSelector = CameraSelector.DefaultBackCamera;

if (cameraSelector == null)
throw new System.Exception("Camera not found");

// The Context here SHOULD be something that's a lifecycle owner
if (Context.Context is AndroidX.Lifecycle.ILifecycleOwner lifecycleOwner)
{
camera = cameraProvider.BindToLifecycle(lifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);
}
else if (Microsoft.Maui.ApplicationModel.Platform.CurrentActivity is AndroidX.Lifecycle.ILifecycleOwner maLifecycleOwner)
{
// if not, this should be sufficient as a fallback
camera = cameraProvider.BindToLifecycle(maLifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);
}
}
}

public void UpdateTorch(bool on)
{
camera?.CameraControl?.EnableTorch(on);
}

public void Focus(Point point)
{

}

public void AutoFocus()
{

}

public void Dispose()
{
cameraExecutor?.Shutdown();
cameraExecutor?.Dispose();
}
}
.SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest)
.Build();

imageAnalyzer.SetAnalyzer(cameraExecutor, new FrameAnalyzer((buffer, size) =>
FrameReady?.Invoke(this, new CameraFrameBufferEventArgs(new Readers.PixelBufferHolder { Data = buffer, Size = size }))));

UpdateCamera();

}), ContextCompat.GetMainExecutor(Context.Context)); //GetMainExecutor: returns an Executor that runs on the main thread.
}

public void Disconnect()
{ }

public void UpdateCamera()
{
if (cameraProvider != null)
{
// Unbind use cases before rebinding
cameraProvider.UnbindAll();

var cameraLocation = CameraLocation;

// Select back camera as a default, or front camera otherwise
if (cameraLocation == CameraLocation.Rear && cameraProvider.HasCamera(CameraSelector.DefaultBackCamera))
cameraSelector = CameraSelector.DefaultBackCamera;
else if (cameraLocation == CameraLocation.Front && cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera))
cameraSelector = CameraSelector.DefaultFrontCamera;
else
cameraSelector = CameraSelector.DefaultBackCamera;

if (cameraSelector == null)
throw new System.Exception("Camera not found");

// The Context here SHOULD be something that's a lifecycle owner
if (Context.Context is AndroidX.Lifecycle.ILifecycleOwner lifecycleOwner)
{
camera = cameraProvider.BindToLifecycle(lifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);
}
else if (Microsoft.Maui.ApplicationModel.Platform.CurrentActivity is AndroidX.Lifecycle.ILifecycleOwner maLifecycleOwner)
{
// if not, this should be sufficient as a fallback
camera = cameraProvider.BindToLifecycle(maLifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);
}
}
}

public void UpdateTorch(bool on)
{
camera?.CameraControl?.EnableTorch(on);
}

public void Focus(Point point)
{

}

public void AutoFocus()
{

}

public void Dispose()
{
DeviceDisplay.MainDisplayInfoChanged -= DeviceDisplay_MainDisplayInfoChanged;
cameraExecutor?.Shutdown();
cameraExecutor?.Dispose();
}
}
}

0 comments on commit b7f8803

Please sign in to comment.