From da0e3ea7414b8590a8e301bacad4a00bedf9de27 Mon Sep 17 00:00:00 2001 From: yushulx Date: Thu, 16 Nov 2023 15:03:33 +0800 Subject: [PATCH] Added scanner callback --- RazorBarcodeLibrary/BarcodeJsInterop.cs | 4 +- RazorBarcodeLibrary/BarcodeScanner.cs | 2 +- .../wwwroot/barcodeJsInterop.js | 18 ++++---- example/Pages/Index.razor | 43 ++++++++++++++++++- example/example.csproj | 5 ++- example/example.csproj.user | 2 +- example/global.json | 5 +++ 7 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 example/global.json diff --git a/RazorBarcodeLibrary/BarcodeJsInterop.cs b/RazorBarcodeLibrary/BarcodeJsInterop.cs index a83a1e5..62a76cd 100644 --- a/RazorBarcodeLibrary/BarcodeJsInterop.cs +++ b/RazorBarcodeLibrary/BarcodeJsInterop.cs @@ -45,10 +45,10 @@ public async Task CreateBarcodeReader() return reader; } - public async Task CreateBarcodeScanner() + public async Task CreateBarcodeScanner(object dotNetObjectReference, string callback) { var module = await moduleTask.Value; - IJSObjectReference jsObjectReference = await module.InvokeAsync("createBarcodeScanner"); + IJSObjectReference jsObjectReference = await module.InvokeAsync("createBarcodeScanner", dotNetObjectReference, callback); BarcodeScanner scanner = new BarcodeScanner(module, jsObjectReference); return scanner; } diff --git a/RazorBarcodeLibrary/BarcodeScanner.cs b/RazorBarcodeLibrary/BarcodeScanner.cs index 9ea827c..69e8608 100644 --- a/RazorBarcodeLibrary/BarcodeScanner.cs +++ b/RazorBarcodeLibrary/BarcodeScanner.cs @@ -19,7 +19,7 @@ public class BarcodeScanner private IJSObjectReference _module; private IJSObjectReference _jsObjectReference; private List _cameras = new List(); - + public BarcodeScanner(IJSObjectReference module, IJSObjectReference scanner) { _module = module; diff --git a/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js b/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js index 9730e6c..bbac7b4 100644 --- a/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js +++ b/RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js @@ -52,11 +52,20 @@ export async function createBarcodeReader() { return null; } -export async function createBarcodeScanner() { +export async function createBarcodeScanner(dotNetHelper, callback) { 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 () { + + } return scanner; } catch (ex) { @@ -154,13 +163,6 @@ export async function openCamera(scanner, cameraInfo) { try { await scanner.setCurrentCamera(cameraInfo); - scanner.onFrameRead = results => { - console.log(results); - }; - scanner.onUnduplicatedRead = (txt, result) => { }; - scanner.onPlayed = function () { - - } await scanner.show(); } catch (ex) { diff --git a/example/Pages/Index.razor b/example/Pages/Index.razor index eac1e52..de584ae 100644 --- a/example/Pages/Index.razor +++ b/example/Pages/Index.razor @@ -46,7 +46,6 @@
-

@result

} else if (_selectedOption == "Barcode Scanner") @@ -70,6 +69,10 @@ } +
+

@result

+
+ @@ -85,6 +88,7 @@ private string _selectedOption = "Barcode Reader"; private string selectedValue { get; set; } = string.Empty; private List cameras = new List(); + private DotNetObjectReference? objRef; public string SelectedOption { @@ -101,6 +105,8 @@ private async Task OnChange() { + result = new MarkupString(""); + if (scanner != null && _selectedOption == "Barcode Reader") { await scanner.CloseCamera(); @@ -172,7 +178,11 @@ version = await barcodeJsInterop.GetVersion(); StateHasChanged(); reader = await barcodeJsInterop.CreateBarcodeReader(); - scanner = await barcodeJsInterop.CreateBarcodeScanner(); + if (objRef != null) + { + scanner = await barcodeJsInterop.CreateBarcodeScanner((object)objRef, "OnResultReady"); + } + isLoading = false; // string parameters = await reader.GetParameters(); // Console.WriteLine(parameters); @@ -220,4 +230,33 @@ isLoading = false; } + + [JSInvokable] + public Task OnResultReady(object message) + { + // if (message == null) return Task.CompletedTask; + List results = BarcodeResult.WrapResult((JsonElement)message); + if (results.Count > 0) + { + string text = ""; + foreach (BarcodeResult result in results) + { + text += "format: " + result.Format + ", "; + text += "text: " + result.Text + "
"; + } + 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 diff --git a/example/example.csproj b/example/example.csproj index edff049..3423d38 100644 --- a/example/example.csproj +++ b/example/example.csproj @@ -8,11 +8,12 @@ - + - + \ No newline at end of file diff --git a/example/example.csproj.user b/example/example.csproj.user index 983ecfc..c404400 100644 --- a/example/example.csproj.user +++ b/example/example.csproj.user @@ -1,7 +1,7 @@  - http + https ProjectDebugger diff --git a/example/global.json b/example/global.json new file mode 100644 index 0000000..24caabe --- /dev/null +++ b/example/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "7.0.401" + } +} \ No newline at end of file