-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from kfrancis/main
Fixes FrameReady for CameraView
- Loading branch information
Showing
2 changed files
with
77 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
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) | ||
=> handler.AutoFocus(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters