Skip to content

Commit 68a54ba

Browse files
authored
Merge pull request #61 from KeesAlderliesten/master
Made it threadsafe
2 parents 80f6e50 + 1c8f64a commit 68a54ba

File tree

4 files changed

+66
-70
lines changed

4 files changed

+66
-70
lines changed

BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public partial class BarcodeReader : ComponentBase, IDisposable, IAsyncDisposabl
7979
public EventCallback<DecodingChangedArgs> OnDecodingChanged { get; set; }
8080

8181
private bool _isDecoding = false;
82+
private DotNetObjectReference<BarcodeReaderInterop>? dotNetHelper;
8283
public bool IsDecoding
8384
{
8485
get => _isDecoding;
@@ -135,17 +136,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
135136
{
136137
if (firstRender) {
137138
_backend = new BarcodeReaderInterop(JSRuntime);
139+
dotNetHelper = DotNetObjectReference.Create(_backend);
140+
await JSRuntime.InvokeVoidAsync("Helpers.setDotNetHelper", dotNetHelper);
138141
try
139142
{
140143
_DecodedPictureCapture = DecodedPictureCapture;
141144
await _backend.SetLastDecodedPictureFormat(DecodedPictureCapture ? "image/jpeg" : null);
142145

143146
await GetVideoInputDevicesAsync();
144147

145-
BarcodeReaderInterop.BarcodeReceived += ReceivedBarcodeText;
146-
BarcodeReaderInterop.ErrorReceived += ReceivedErrorMessage;
147-
BarcodeReaderInterop.DecodingStarted += DecodingStarted;
148-
BarcodeReaderInterop.DecodingStopped += DecodingStopped;
148+
_backend.BarcodeReceived += ReceivedBarcodeText;
149+
_backend.ErrorReceived += ReceivedErrorMessage;
150+
_backend.DecodingStarted += DecodingStarted;
151+
_backend.DecodingStopped += DecodingStopped;
149152

150153
if (StartCameraAutomatically && _videoInputDevices.Count > 0)
151154
{
@@ -179,11 +182,11 @@ public async ValueTask DisposeAsync()
179182
try
180183
{
181184
await StopDecoding();
182-
183-
BarcodeReaderInterop.BarcodeReceived -= ReceivedBarcodeText;
184-
BarcodeReaderInterop.ErrorReceived -= ReceivedErrorMessage;
185-
BarcodeReaderInterop.DecodingStarted -= DecodingStarted;
186-
BarcodeReaderInterop.DecodingStopped -= DecodingStopped;
185+
186+
_backend.BarcodeReceived -= ReceivedBarcodeText;
187+
_backend.ErrorReceived -= ReceivedErrorMessage;
188+
_backend.DecodingStarted -= DecodingStarted;
189+
_backend.DecodingStopped -= DecodingStopped;
187190
}
188191
catch (Exception ex)
189192
{
@@ -238,7 +241,7 @@ public async Task<string> CaptureLastDecodedPicture()
238241

239242
public async Task StopDecoding()
240243
{
241-
BarcodeReaderInterop.OnBarcodeReceived(string.Empty);
244+
_backend.OnBarcodeReceived(string.Empty);
242245
await _backend.StopDecoding();
243246
StateHasChanged();
244247
}

BlazorBarcodeScanner.ZXing.JS/BarcodeReaderInterop.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ private async Task<string> PictureGetMarshalled(string source)
132132
return await jSRuntime.InvokeAsync<string>("BlazorBarcodeScanner.pictureGetBase64", source);
133133
}
134134

135-
private static string lastCode = string.Empty;
136-
public static void OnBarcodeReceived(string barcodeText)
135+
private string lastCode = string.Empty;
136+
[JSInvokable]
137+
public void OnBarcodeReceived(string barcodeText)
137138
{
138139
if (string.IsNullOrEmpty(barcodeText))
139140
{
@@ -153,7 +154,8 @@ public static void OnBarcodeReceived(string barcodeText)
153154

154155
BarcodeReceived?.Invoke(args);
155156
}
156-
public static void OnErrorReceived(Exception exception)
157+
[JSInvokable]
158+
public void OnErrorReceived(Exception exception)
157159
{
158160
if (string.IsNullOrEmpty(exception.Message))
159161
{
@@ -167,16 +169,17 @@ public static void OnErrorReceived(Exception exception)
167169

168170
ErrorReceived?.Invoke(args);
169171
}
170-
171-
public static void OnNotFoundReceived()
172+
[JSInvokable]
173+
public void OnNotFoundReceived()
172174
{
173175
if (!string.IsNullOrEmpty(lastCode))
174176
{
175177
lastCode = string.Empty;
176178
BarcodeNotFound?.Invoke();
177179
}
178180
}
179-
public static void OnDecodingStarted(string deviceId)
181+
[JSInvokable]
182+
public void OnDecodingStarted(string deviceId)
180183
{
181184
if (string.IsNullOrEmpty(deviceId))
182185
{
@@ -190,8 +193,8 @@ public static void OnDecodingStarted(string deviceId)
190193

191194
DecodingStarted?.Invoke(args);
192195
}
193-
194-
public static void OnDecodingStopped(string deviceId)
196+
[JSInvokable]
197+
public void OnDecodingStopped(string deviceId)
195198
{
196199
if (string.IsNullOrEmpty(deviceId))
197200
{
@@ -206,13 +209,13 @@ public static void OnDecodingStopped(string deviceId)
206209
DecodingStopped?.Invoke(args);
207210
}
208211

209-
public static event BarcodeReceivedEventHandler BarcodeReceived;
210-
public static event ErrorReceivedEventHandler ErrorReceived;
212+
public event BarcodeReceivedEventHandler BarcodeReceived;
213+
public event ErrorReceivedEventHandler ErrorReceived;
211214

212-
public static event DecodingStartedEventHandler DecodingStarted;
213-
public static event DecodingStoppedEventHandler DecodingStopped;
215+
public event DecodingStartedEventHandler DecodingStarted;
216+
public event DecodingStoppedEventHandler DecodingStopped;
214217

215-
public static event Action BarcodeNotFound;
218+
public event Action BarcodeNotFound;
216219
}
217220
public class ErrorReceivedEventArgs : EventArgs {
218221
public string Message { get; set; }

BlazorBarcodeScanner.ZXing.JS/JsInteropClass.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

BlazorBarcodeScanner.ZXing.JS/wwwroot/BlazorBarcodeScanner.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
console.log("Init BlazorBarcodeScanner");
2+
3+
class Helpers {
4+
static dotNetHelper;
5+
6+
static setDotNetHelper(value) {
7+
Helpers.dotNetHelper = value;
8+
}
9+
10+
static async receiveBarcode(text) {
11+
await Helpers.dotNetHelper.invokeMethodAsync('OnBarcodeReceived', text);
12+
}
13+
14+
static async receiveError(err) {
15+
await Helpers.dotNetHelper.invokeMethodAsync('OnErrorReceived', err);
16+
}
17+
18+
static async receiveNotFound() {
19+
await Helpers.dotNetHelper.invokeMethodAsync('OnNotFoundReceived');
20+
}
21+
22+
static async decodingStarted(deviceId) {
23+
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStarted', deviceId);
24+
}
25+
26+
static async decodingStopped(deviceId) {
27+
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStopped', deviceId);
28+
}
29+
}
30+
31+
window.Helpers = Helpers;
32+
233
async function mediaStreamSetTorch(track, onOff) {
334
await track.applyConstraints({
435
advanced: [{
@@ -99,20 +130,20 @@ window.BlazorBarcodeScanner = {
99130
if (this.lastPictureDecodedFormat) {
100131
this.lastPictureDecoded = this.codeReader.captureCanvas.toDataURL(this.lastPictureDecodedFormat);
101132
}
102-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', result.text)
133+
Helpers.receiveBarcode(result.text)
103134
.then(message => {
104135
console.log(message);
105136
});
106137
}
107138
if (err && !(err instanceof ZXing.NotFoundException)) {
108-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveError', err)
139+
Helpers.receiveError(err)
109140
.then(message => {
110141
console.log(message);
111142
});
112143
}
113144
if (err && (err instanceof ZXing.NotFoundException)) {
114145
this.lastPictureDecoded = undefined;
115-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveNotFound');
146+
Helpers.receiveNotFound();
116147
}
117148
});
118149

@@ -123,15 +154,15 @@ window.BlazorBarcodeScanner = {
123154
advanced: [{ torch: true }] // or false to turn off the torch
124155
}); */
125156
console.log(`Started continous decode from camera with id ${this.selectedDeviceId}`);
126-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStarted', this.selectedDeviceId)
157+
Helpers.decodingStarted(this.selectedDeviceId)
127158
},
128159
stopDecoding: function () {
129160
this.codeReader.reset();
130-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', '')
161+
Helpers.receiveBarcode('')
131162
.then(message => {
132163
console.log(message);
133164
});
134-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStopped', this.selectedDeviceId)
165+
Helpers.decodingStopped(this.selectedDeviceId)
135166
console.log('Reset camera stream.');
136167
},
137168
setTorchOn: function () {

0 commit comments

Comments
 (0)