Skip to content

Commit

Permalink
Added scanner callback
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Nov 16, 2023
1 parent 9f99f1c commit da0e3ea
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions RazorBarcodeLibrary/BarcodeJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public async Task<BarcodeReader> CreateBarcodeReader()
return reader;
}

public async Task<BarcodeScanner> CreateBarcodeScanner()
public async Task<BarcodeScanner> CreateBarcodeScanner(object dotNetObjectReference, string callback)
{
var module = await moduleTask.Value;
IJSObjectReference jsObjectReference = await module.InvokeAsync<IJSObjectReference>("createBarcodeScanner");
IJSObjectReference jsObjectReference = await module.InvokeAsync<IJSObjectReference>("createBarcodeScanner", dotNetObjectReference, callback);
BarcodeScanner scanner = new BarcodeScanner(module, jsObjectReference);
return scanner;
}
Expand Down
2 changes: 1 addition & 1 deletion RazorBarcodeLibrary/BarcodeScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BarcodeScanner
private IJSObjectReference _module;
private IJSObjectReference _jsObjectReference;
private List<Camera> _cameras = new List<Camera>();

public BarcodeScanner(IJSObjectReference module, IJSObjectReference scanner)
{
_module = module;
Expand Down
18 changes: 10 additions & 8 deletions RazorBarcodeLibrary/wwwroot/barcodeJsInterop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 41 additions & 2 deletions example/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

<div>
<button @onclick="Decode">Read Barcode</button>
<p>@result</p>
</div>
}
else if (_selectedOption == "Barcode Scanner")
Expand All @@ -70,6 +69,10 @@
<canvas id="videoOverlay"></canvas>
</div>
}
<div>
<p>@result</p>
</div>

</div>
</div>

Expand All @@ -85,6 +88,7 @@
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 @@ -101,6 +105,8 @@

private async Task OnChange()
{
result = new MarkupString("");

if (scanner != null && _selectedOption == "Barcode Reader")
{
await scanner.CloseCamera();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -220,4 +230,33 @@

isLoading = false;
}

[JSInvokable]
public Task OnResultReady(object message)
{
// if (message == null) return Task.CompletedTask;
List<BarcodeResult> results = BarcodeResult.WrapResult((JsonElement)message);
if (results.Count > 0)
{
string text = "";
foreach (BarcodeResult result in results)
{
text += "format: " + result.Format + ", ";
text += "text: " + result.Text + "<br>";
}
result = new MarkupString(text);
}
StateHasChanged();
return Task.CompletedTask;
}

protected override void OnInitialized()
{
objRef = DotNetObjectReference.Create(this);
}

public void Dispose()
{
objRef?.Dispose();
}
}
5 changes: 3 additions & 2 deletions example/example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
Version="7.0.11" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RazorBarcodeLibrary\RazorBarcodeLibrary.csproj" />
</ItemGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion example/example.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>http</ActiveDebugProfile>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
Expand Down
5 changes: 5 additions & 0 deletions example/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "7.0.401"
}
}

0 comments on commit da0e3ea

Please sign in to comment.