Skip to content

Commit

Permalink
Merge pull request #61 from KeesAlderliesten/master
Browse files Browse the repository at this point in the history
Made it threadsafe
  • Loading branch information
sabitertan authored Nov 18, 2023
2 parents 80f6e50 + 1c8f64a commit 68a54ba
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 70 deletions.
23 changes: 13 additions & 10 deletions BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public partial class BarcodeReader : ComponentBase, IDisposable, IAsyncDisposabl
public EventCallback<DecodingChangedArgs> OnDecodingChanged { get; set; }

private bool _isDecoding = false;
private DotNetObjectReference<BarcodeReaderInterop>? dotNetHelper;

Check warning on line 82 in BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs

View workflow job for this annotation

GitHub Actions / deploy-to-github-pages

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public bool IsDecoding
{
get => _isDecoding;
Expand Down Expand Up @@ -135,17 +136,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender) {
_backend = new BarcodeReaderInterop(JSRuntime);
dotNetHelper = DotNetObjectReference.Create(_backend);
await JSRuntime.InvokeVoidAsync("Helpers.setDotNetHelper", dotNetHelper);
try
{
_DecodedPictureCapture = DecodedPictureCapture;
await _backend.SetLastDecodedPictureFormat(DecodedPictureCapture ? "image/jpeg" : null);

await GetVideoInputDevicesAsync();

BarcodeReaderInterop.BarcodeReceived += ReceivedBarcodeText;
BarcodeReaderInterop.ErrorReceived += ReceivedErrorMessage;
BarcodeReaderInterop.DecodingStarted += DecodingStarted;
BarcodeReaderInterop.DecodingStopped += DecodingStopped;
_backend.BarcodeReceived += ReceivedBarcodeText;
_backend.ErrorReceived += ReceivedErrorMessage;
_backend.DecodingStarted += DecodingStarted;
_backend.DecodingStopped += DecodingStopped;

if (StartCameraAutomatically && _videoInputDevices.Count > 0)
{
Expand Down Expand Up @@ -179,11 +182,11 @@ public async ValueTask DisposeAsync()
try
{
await StopDecoding();
BarcodeReaderInterop.BarcodeReceived -= ReceivedBarcodeText;
BarcodeReaderInterop.ErrorReceived -= ReceivedErrorMessage;
BarcodeReaderInterop.DecodingStarted -= DecodingStarted;
BarcodeReaderInterop.DecodingStopped -= DecodingStopped;

_backend.BarcodeReceived -= ReceivedBarcodeText;
_backend.ErrorReceived -= ReceivedErrorMessage;
_backend.DecodingStarted -= DecodingStarted;
_backend.DecodingStopped -= DecodingStopped;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -238,7 +241,7 @@ public async Task<string> CaptureLastDecodedPicture()

public async Task StopDecoding()
{
BarcodeReaderInterop.OnBarcodeReceived(string.Empty);
_backend.OnBarcodeReceived(string.Empty);
await _backend.StopDecoding();
StateHasChanged();
}
Expand Down
29 changes: 16 additions & 13 deletions BlazorBarcodeScanner.ZXing.JS/BarcodeReaderInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ private async Task<string> PictureGetMarshalled(string source)
return await jSRuntime.InvokeAsync<string>("BlazorBarcodeScanner.pictureGetBase64", source);
}

private static string lastCode = string.Empty;
public static void OnBarcodeReceived(string barcodeText)
private string lastCode = string.Empty;
[JSInvokable]
public void OnBarcodeReceived(string barcodeText)
{
if (string.IsNullOrEmpty(barcodeText))
{
Expand All @@ -153,7 +154,8 @@ public static void OnBarcodeReceived(string barcodeText)

BarcodeReceived?.Invoke(args);
}
public static void OnErrorReceived(Exception exception)
[JSInvokable]
public void OnErrorReceived(Exception exception)
{
if (string.IsNullOrEmpty(exception.Message))
{
Expand All @@ -167,16 +169,17 @@ public static void OnErrorReceived(Exception exception)

ErrorReceived?.Invoke(args);
}

public static void OnNotFoundReceived()
[JSInvokable]
public void OnNotFoundReceived()
{
if (!string.IsNullOrEmpty(lastCode))
{
lastCode = string.Empty;
BarcodeNotFound?.Invoke();
}
}
public static void OnDecodingStarted(string deviceId)
[JSInvokable]
public void OnDecodingStarted(string deviceId)
{
if (string.IsNullOrEmpty(deviceId))
{
Expand All @@ -190,8 +193,8 @@ public static void OnDecodingStarted(string deviceId)

DecodingStarted?.Invoke(args);
}

public static void OnDecodingStopped(string deviceId)
[JSInvokable]
public void OnDecodingStopped(string deviceId)
{
if (string.IsNullOrEmpty(deviceId))
{
Expand All @@ -206,13 +209,13 @@ public static void OnDecodingStopped(string deviceId)
DecodingStopped?.Invoke(args);
}

public static event BarcodeReceivedEventHandler BarcodeReceived;
public static event ErrorReceivedEventHandler ErrorReceived;
public event BarcodeReceivedEventHandler BarcodeReceived;
public event ErrorReceivedEventHandler ErrorReceived;

public static event DecodingStartedEventHandler DecodingStarted;
public static event DecodingStoppedEventHandler DecodingStopped;
public event DecodingStartedEventHandler DecodingStarted;
public event DecodingStoppedEventHandler DecodingStopped;

public static event Action BarcodeNotFound;
public event Action BarcodeNotFound;
}
public class ErrorReceivedEventArgs : EventArgs {
public string Message { get; set; }
Expand Down
41 changes: 0 additions & 41 deletions BlazorBarcodeScanner.ZXing.JS/JsInteropClass.cs

This file was deleted.

43 changes: 37 additions & 6 deletions BlazorBarcodeScanner.ZXing.JS/wwwroot/BlazorBarcodeScanner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
console.log("Init BlazorBarcodeScanner");

class Helpers {
static dotNetHelper;

static setDotNetHelper(value) {
Helpers.dotNetHelper = value;
}

static async receiveBarcode(text) {
await Helpers.dotNetHelper.invokeMethodAsync('OnBarcodeReceived', text);
}

static async receiveError(err) {
await Helpers.dotNetHelper.invokeMethodAsync('OnErrorReceived', err);
}

static async receiveNotFound() {
await Helpers.dotNetHelper.invokeMethodAsync('OnNotFoundReceived');
}

static async decodingStarted(deviceId) {
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStarted', deviceId);
}

static async decodingStopped(deviceId) {
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStopped', deviceId);
}
}

window.Helpers = Helpers;

async function mediaStreamSetTorch(track, onOff) {
await track.applyConstraints({
advanced: [{
Expand Down Expand Up @@ -99,20 +130,20 @@ window.BlazorBarcodeScanner = {
if (this.lastPictureDecodedFormat) {
this.lastPictureDecoded = this.codeReader.captureCanvas.toDataURL(this.lastPictureDecodedFormat);
}
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', result.text)
Helpers.receiveBarcode(result.text)
.then(message => {
console.log(message);
});
}
if (err && !(err instanceof ZXing.NotFoundException)) {
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveError', err)
Helpers.receiveError(err)
.then(message => {
console.log(message);
});
}
if (err && (err instanceof ZXing.NotFoundException)) {
this.lastPictureDecoded = undefined;
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveNotFound');
Helpers.receiveNotFound();
}
});

Expand All @@ -123,15 +154,15 @@ window.BlazorBarcodeScanner = {
advanced: [{ torch: true }] // or false to turn off the torch
}); */
console.log(`Started continous decode from camera with id ${this.selectedDeviceId}`);
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStarted', this.selectedDeviceId)
Helpers.decodingStarted(this.selectedDeviceId)
},
stopDecoding: function () {
this.codeReader.reset();
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', '')
Helpers.receiveBarcode('')
.then(message => {
console.log(message);
});
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStopped', this.selectedDeviceId)
Helpers.decodingStopped(this.selectedDeviceId)
console.log('Reset camera stream.');
},
setTorchOn: function () {
Expand Down

0 comments on commit 68a54ba

Please sign in to comment.