Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes FrameReady for CameraView #135

Merged
merged 7 commits into from
Oct 23, 2023
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
149 changes: 75 additions & 74 deletions ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,105 @@
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using System;
using System.Linq;

namespace ZXing.Net.Maui
{
public partial class CameraBarcodeReaderViewHandler : ViewHandler<ICameraBarcodeReaderView, NativePlatformCameraPreviewView>
{
public static PropertyMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderViewMapper = new()
{
[nameof(ICameraBarcodeReaderView.Options)] = MapOptions,
[nameof(ICameraBarcodeReaderView.IsDetecting)] = MapIsDetecting,
[nameof(ICameraBarcodeReaderView.IsTorchOn)] = (handler, virtualView) => handler.cameraManager.UpdateTorch(virtualView.IsTorchOn),
[nameof(ICameraBarcodeReaderView.CameraLocation)] = (handler, virtualView) => handler.cameraManager.UpdateCameraLocation(virtualView.CameraLocation)
};
public partial class CameraBarcodeReaderViewHandler : ViewHandler<ICameraBarcodeReaderView, NativePlatformCameraPreviewView>
{
public static PropertyMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderViewMapper = new()
{
[nameof(ICameraBarcodeReaderView.Options)] = MapOptions,
[nameof(ICameraBarcodeReaderView.IsDetecting)] = MapIsDetecting,
[nameof(ICameraBarcodeReaderView.IsTorchOn)] = (handler, virtualView) => handler.cameraManager.UpdateTorch(virtualView.IsTorchOn),
[nameof(ICameraBarcodeReaderView.CameraLocation)] = (handler, virtualView) => handler.cameraManager.UpdateCameraLocation(virtualView.CameraLocation)
};

public static CommandMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderCommandMapper = new()
{
[nameof(ICameraBarcodeReaderView.Focus)] = MapFocus,
[nameof(ICameraBarcodeReaderView.AutoFocus)] = MapAutoFocus,
};
public static CommandMapper<ICameraBarcodeReaderView, CameraBarcodeReaderViewHandler> CameraBarcodeReaderCommandMapper = new()
{
[nameof(ICameraBarcodeReaderView.Focus)] = MapFocus,
[nameof(ICameraBarcodeReaderView.AutoFocus)] = MapAutoFocus,
};

public CameraBarcodeReaderViewHandler() : base(CameraBarcodeReaderViewMapper)
{
}
public CameraBarcodeReaderViewHandler() : base(CameraBarcodeReaderViewMapper, CameraBarcodeReaderCommandMapper)
{
}

public CameraBarcodeReaderViewHandler(PropertyMapper mapper = null) : base(mapper ?? CameraBarcodeReaderViewMapper)
{
}
public CameraBarcodeReaderViewHandler(PropertyMapper propertyMapper = null, CommandMapper commandMapper = null)
: base(propertyMapper ?? CameraBarcodeReaderViewMapper, commandMapper ?? CameraBarcodeReaderCommandMapper)
{
}

CameraManager cameraManager;
CameraManager cameraManager;

Readers.IBarcodeReader barcodeReader;
Readers.IBarcodeReader barcodeReader;

protected Readers.IBarcodeReader BarcodeReader
=> barcodeReader ??= Services.GetService<Readers.IBarcodeReader>();
protected Readers.IBarcodeReader BarcodeReader
=> barcodeReader ??= Services.GetService<Readers.IBarcodeReader>();

protected override NativePlatformCameraPreviewView CreatePlatformView()
{
if (cameraManager == null)
cameraManager = new(MauiContext, VirtualView?.CameraLocation ?? CameraLocation.Rear);
var v = cameraManager.CreateNativeView();
return v;
}
protected override NativePlatformCameraPreviewView CreatePlatformView()
{
if (cameraManager == null)
cameraManager = new(MauiContext, VirtualView?.CameraLocation ?? CameraLocation.Rear);
var v = cameraManager.CreateNativeView();
return v;
}

protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView)
{
base.ConnectHandler(nativeView);
protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView)
{
base.ConnectHandler(nativeView);

if (await cameraManager.CheckPermissions())
cameraManager.Connect();
if (await cameraManager.CheckPermissions())
cameraManager.Connect();

cameraManager.FrameReady += CameraManager_FrameReady;
}
cameraManager.FrameReady += CameraManager_FrameReady;
}

protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
{
cameraManager.FrameReady -= CameraManager_FrameReady;
protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
{
cameraManager.FrameReady -= CameraManager_FrameReady;

cameraManager.Disconnect();
cameraManager.Disconnect();

base.DisconnectHandler(nativeView);
}
base.DisconnectHandler(nativeView);
}

private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e)
{
VirtualView?.FrameReady(e);
private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e)
{
VirtualView?.FrameReady(e);

if (VirtualView.IsDetecting)
{
var barcodes = BarcodeReader.Decode(e.Data);
if (VirtualView.IsDetecting)
{
var barcodes = BarcodeReader.Decode(e.Data);

if (barcodes?.Any() ?? false)
VirtualView?.BarcodesDetected(new BarcodeDetectionEventArgs(barcodes));
}
}
if (barcodes?.Any() ?? false)
VirtualView?.BarcodesDetected(new BarcodeDetectionEventArgs(barcodes));
}
}

public static void MapOptions(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView)
=> handler.BarcodeReader.Options = cameraBarcodeReaderView.Options;
public static void MapOptions(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView)
=> handler.BarcodeReader.Options = cameraBarcodeReaderView.Options;

public static void MapIsDetecting(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView)
{ }
public static void MapIsDetecting(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView)
{ }

public void Focus(Point point)
=> cameraManager?.Focus(point);
public void Focus(Point point)
=> cameraManager?.Focus(point);

public void AutoFocus()
=> cameraManager?.AutoFocus();
public void AutoFocus()
=> cameraManager?.AutoFocus();

public static void MapFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameter)
{
if (parameter is not Point point)
throw new ArgumentException("Invalid parameter", "point");
public static void MapFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameter)

Check warning on line 94 in ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 94 in ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 94 in ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (parameter is not Point point)
throw new ArgumentException("Invalid parameter", "point");

handler.Focus(point);
}
handler.Focus(point);
}

public static void MapAutoFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameters)
=> handler.AutoFocus();
}
public static void MapAutoFocus(CameraBarcodeReaderViewHandler handler, ICameraBarcodeReaderView cameraBarcodeReaderView, object? parameters)

Check warning on line 102 in ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 102 in ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
=> handler.AutoFocus();
}
}
6 changes: 2 additions & 4 deletions ZXing.Net.MAUI/CameraViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

CameraManager cameraManager;

public event EventHandler<CameraFrameBufferEventArgs> FrameReady;

public CameraViewHandler() : base(CameraViewMapper)
{
}
Expand Down Expand Up @@ -54,9 +52,9 @@
}

void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e)
=> FrameReady?.Invoke(this, e);
=> VirtualView?.FrameReady(e);

protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView)
{
cameraManager.FrameReady -= CameraManager_FrameReady;

Expand All @@ -74,7 +72,7 @@
public void AutoFocus()
=> cameraManager?.AutoFocus();

public static void MapFocus(CameraViewHandler handler, ICameraView cameraBarcodeReaderView, object? parameter)

Check warning on line 75 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 75 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 75 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (parameter is not Point point)
throw new ArgumentException("Invalid parameter", "point");
Expand All @@ -82,7 +80,7 @@
handler.Focus(point);
}

public static void MapAutoFocus(CameraViewHandler handler, ICameraView cameraBarcodeReaderView, object? parameters)

Check warning on line 83 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 83 in ZXing.Net.MAUI/CameraViewHandler.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
=> handler.AutoFocus();
}
}
Loading