Skip to content

Commit

Permalink
Optimized scanner callback
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Nov 16, 2023
1 parent da0e3ea commit 7b4230b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 52 deletions.
5 changes: 2 additions & 3 deletions RazorBarcodeLibrary/BarcodeJsInterop.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.JSInterop;
using System.Text.Json;

namespace RazorBarcodeLibrary
{
Expand Down Expand Up @@ -45,10 +44,10 @@ public async Task<BarcodeReader> CreateBarcodeReader()
return reader;
}

public async Task<BarcodeScanner> CreateBarcodeScanner(object dotNetObjectReference, string callback)
public async Task<BarcodeScanner> CreateBarcodeScanner()
{
var module = await moduleTask.Value;
IJSObjectReference jsObjectReference = await module.InvokeAsync<IJSObjectReference>("createBarcodeScanner", dotNetObjectReference, callback);
IJSObjectReference jsObjectReference = await module.InvokeAsync<IJSObjectReference>("createBarcodeScanner");
BarcodeScanner scanner = new BarcodeScanner(module, jsObjectReference);
return scanner;
}
Expand Down
10 changes: 1 addition & 9 deletions RazorBarcodeLibrary/BarcodeReader.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
2 changes: 0 additions & 2 deletions RazorBarcodeLibrary/BarcodeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ public static List<BarcodeResult> WrapResult(JsonElement? result)
barcodeResult.Y4 = intValue;
}

Console.WriteLine(barcodeResult.ToString());

}

results.Add(barcodeResult);
Expand Down
50 changes: 43 additions & 7 deletions RazorBarcodeLibrary/BarcodeScanner.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<Camera> _cameras = new List<Camera>();
private ICallback? _callback;
DotNetObjectReference<BarcodeScanner> 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)
Expand Down Expand Up @@ -78,5 +76,43 @@ public async Task<List<Camera>> GetCameras()

return _cameras;
}

public interface ICallback
{
void OnCallback(List<BarcodeResult> results);
}

[JSInvokable]
public Task OnResultReady(object message)
{
List<BarcodeResult> 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();
}
}
}
21 changes: 15 additions & 6 deletions RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

Expand All @@ -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;
Expand Down
35 changes: 10 additions & 25 deletions example/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@inject IJSRuntime JSRuntime
@using System.Text.Json
@using RazorBarcodeLibrary
@implements BarcodeScanner.ICallback

<PageTitle>Index</PageTitle>

Expand Down Expand Up @@ -72,8 +73,8 @@
<div>
<p>@result</p>
</div>
</div>

</div>
</div>

@code {
Expand All @@ -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<Camera> cameras = new List<Camera>();
private DotNetObjectReference<Index>? objRef;

public string SelectedOption
{
Expand All @@ -98,7 +99,7 @@
if (_selectedOption != value)
{
_selectedOption = value;
var _ = OnChange();
var _ = OnChange();
}
}
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -231,11 +230,8 @@
isLoading = false;
}

[JSInvokable]
public Task OnResultReady(object message)
public void OnCallback(List<BarcodeResult> results)
{
// if (message == null) return Task.CompletedTask;
List<BarcodeResult> results = BarcodeResult.WrapResult((JsonElement)message);
if (results.Count > 0)
{
string text = "";
Expand All @@ -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();
}
}

0 comments on commit 7b4230b

Please sign in to comment.