From 7b4230b6a365fba4012f07baaacdc19d84872145 Mon Sep 17 00:00:00 2001 From: yushulx Date: Thu, 16 Nov 2023 16:18:50 +0800 Subject: [PATCH] Optimized scanner callback --- RazorBarcodeLibrary/BarcodeJsInterop.cs | 5 +- RazorBarcodeLibrary/BarcodeReader.cs | 10 +--- RazorBarcodeLibrary/BarcodeResult.cs | 2 - RazorBarcodeLibrary/BarcodeScanner.cs | 50 ++++++++++++++++--- .../wwwroot/barcodeJsInterop.js | 21 +++++--- example/Pages/Index.razor | 35 ++++--------- 6 files changed, 71 insertions(+), 52 deletions(-) diff --git a/RazorBarcodeLibrary/BarcodeJsInterop.cs b/RazorBarcodeLibrary/BarcodeJsInterop.cs index 62a76cd..21ac543 100644 --- a/RazorBarcodeLibrary/BarcodeJsInterop.cs +++ b/RazorBarcodeLibrary/BarcodeJsInterop.cs @@ -1,5 +1,4 @@ using Microsoft.JSInterop; -using System.Text.Json; namespace RazorBarcodeLibrary { @@ -45,10 +44,10 @@ public async Task CreateBarcodeReader() return reader; } - public async Task CreateBarcodeScanner(object dotNetObjectReference, string callback) + public async Task CreateBarcodeScanner() { var module = await moduleTask.Value; - IJSObjectReference jsObjectReference = await module.InvokeAsync("createBarcodeScanner", dotNetObjectReference, callback); + IJSObjectReference jsObjectReference = await module.InvokeAsync("createBarcodeScanner"); BarcodeScanner scanner = new BarcodeScanner(module, jsObjectReference); return scanner; } diff --git a/RazorBarcodeLibrary/BarcodeReader.cs b/RazorBarcodeLibrary/BarcodeReader.cs index 8d55d84..47ca208 100644 --- a/RazorBarcodeLibrary/BarcodeReader.cs +++ b/RazorBarcodeLibrary/BarcodeReader.cs @@ -1,13 +1,5 @@ -using Microsoft.AspNetCore.Components; -using Microsoft.JSInterop; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.PortableExecutable; -using System.Text; +using Microsoft.JSInterop; using System.Text.Json; -using System.Threading.Tasks; namespace RazorBarcodeLibrary { diff --git a/RazorBarcodeLibrary/BarcodeResult.cs b/RazorBarcodeLibrary/BarcodeResult.cs index 2493457..dcbd547 100644 --- a/RazorBarcodeLibrary/BarcodeResult.cs +++ b/RazorBarcodeLibrary/BarcodeResult.cs @@ -122,8 +122,6 @@ public static List WrapResult(JsonElement? result) barcodeResult.Y4 = intValue; } - Console.WriteLine(barcodeResult.ToString()); - } results.Add(barcodeResult); diff --git a/RazorBarcodeLibrary/BarcodeScanner.cs b/RazorBarcodeLibrary/BarcodeScanner.cs index 69e8608..de5c753 100644 --- a/RazorBarcodeLibrary/BarcodeScanner.cs +++ b/RazorBarcodeLibrary/BarcodeScanner.cs @@ -1,11 +1,5 @@ using Microsoft.JSInterop; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.Json; -using System.Threading.Tasks; -using System.Xml.Linq; namespace RazorBarcodeLibrary { @@ -14,16 +8,20 @@ public class Camera public string DeviceId { get; set; } = string.Empty; public string Label { get; set; } = string.Empty; } - public class BarcodeScanner + public class BarcodeScanner : IDisposable { private IJSObjectReference _module; private IJSObjectReference _jsObjectReference; private List _cameras = new List(); + private ICallback? _callback; + DotNetObjectReference objRef; + private bool _disposed = false; public BarcodeScanner(IJSObjectReference module, IJSObjectReference scanner) { _module = module; _jsObjectReference = scanner; + objRef = DotNetObjectReference.Create(this); } public async Task SetVideoElement(string videoId) @@ -78,5 +76,43 @@ public async Task> GetCameras() return _cameras; } + + public interface ICallback + { + void OnCallback(List results); + } + + [JSInvokable] + public Task OnResultReady(object message) + { + List results = BarcodeResult.WrapResult((JsonElement)message); + if (_callback != null) + { + _callback.OnCallback(results); + } + + return Task.CompletedTask; + } + + public async Task RegisterCallback(ICallback callback) + { + _callback = callback; + await _module.InvokeVoidAsync("registerCallback", _jsObjectReference, objRef, "OnResultReady"); + } + + public void Dispose() + { + if (_disposed == false) + { + objRef.Dispose(); + _disposed = true; + } + } + + ~BarcodeScanner() + { + if (_disposed == false) + Dispose(); + } } } diff --git a/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js b/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js index bbac7b4..b66aca4 100644 --- a/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js +++ b/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js @@ -52,16 +52,11 @@ export async function createBarcodeReader() { return null; } -export async function createBarcodeScanner(dotNetHelper, callback) { +export async function createBarcodeScanner() { if (!Dynamsoft) return; try { let scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance(); - scanner.onFrameRead = results => { - //console.log(results); - - dotNetHelper.invokeMethodAsync(callback, results); - }; scanner.onUnduplicatedRead = (txt, result) => { }; scanner.onPlayed = function () { @@ -74,6 +69,20 @@ export async function createBarcodeScanner(dotNetHelper, callback) { return null; } +export async function registerCallback(scanner, dotNetHelper, callback) { + if (!Dynamsoft) return; + + try { + scanner.onFrameRead = results => { + dotNetHelper.invokeMethodAsync(callback, results); + }; + } + catch (ex) { + console.error(ex); + } + return null; +} + export function drawCanvas(canvasId, sourceWidth, sourceHeight, results) { var canvas = document.getElementById(canvasId); if (!canvas) return; diff --git a/example/Pages/Index.razor b/example/Pages/Index.razor index de584ae..c74fe0d 100644 --- a/example/Pages/Index.razor +++ b/example/Pages/Index.razor @@ -2,6 +2,7 @@ @inject IJSRuntime JSRuntime @using System.Text.Json @using RazorBarcodeLibrary +@implements BarcodeScanner.ICallback Index @@ -72,8 +73,8 @@

@result

- - + + @code { @@ -84,11 +85,11 @@ private MarkupString result; private string version = "N/A"; private string? imageSrc; - private string licenseKey = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; + private string licenseKey = + "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; private string _selectedOption = "Barcode Reader"; private string selectedValue { get; set; } = string.Empty; private List cameras = new List(); - private DotNetObjectReference? objRef; public string SelectedOption { @@ -98,7 +99,7 @@ if (_selectedOption != value) { _selectedOption = value; - var _ = OnChange(); + var _ = OnChange(); } } } @@ -118,7 +119,7 @@ result = new MarkupString(""); if (barcodeJsInterop != null) await barcodeJsInterop.ClearCanvas("overlay"); var imageFiles = e.GetMultipleFiles(); - var format = "image/png"; + var format = "image/png"; if (imageFiles.Count > 0) { @@ -178,10 +179,8 @@ version = await barcodeJsInterop.GetVersion(); StateHasChanged(); reader = await barcodeJsInterop.CreateBarcodeReader(); - if (objRef != null) - { - scanner = await barcodeJsInterop.CreateBarcodeScanner((object)objRef, "OnResultReady"); - } + scanner = await barcodeJsInterop.CreateBarcodeScanner(); + await scanner.RegisterCallback(this); isLoading = false; // string parameters = await reader.GetParameters(); @@ -231,11 +230,8 @@ isLoading = false; } - [JSInvokable] - public Task OnResultReady(object message) + public void OnCallback(List results) { - // if (message == null) return Task.CompletedTask; - List results = BarcodeResult.WrapResult((JsonElement)message); if (results.Count > 0) { string text = ""; @@ -247,16 +243,5 @@ result = new MarkupString(text); } StateHasChanged(); - return Task.CompletedTask; - } - - protected override void OnInitialized() - { - objRef = DotNetObjectReference.Create(this); - } - - public void Dispose() - { - objRef?.Dispose(); } } \ No newline at end of file