Skip to content

Commit

Permalink
Merge pull request #135 from kfrancis/main
Browse files Browse the repository at this point in the history
Fixes FrameReady for CameraView
  • Loading branch information
Redth authored Oct 23, 2023
2 parents 20fe0f8 + 143128e commit ea0f293
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 78 deletions.
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.

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.

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.

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.

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.

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.

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.

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 @@ public partial class CameraViewHandler : ViewHandler<ICameraView, NativePlatform

CameraManager cameraManager;

public event EventHandler<CameraFrameBufferEventArgs> FrameReady;

public CameraViewHandler() : base(CameraViewMapper)
{
}
Expand Down Expand Up @@ -54,9 +52,9 @@ protected override async void ConnectHandler(NativePlatformCameraPreviewView nat
}

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 Down

0 comments on commit ea0f293

Please sign in to comment.