Skip to content

Commit 8cefe65

Browse files
committed
Add option Decodeonce : decodeOnce or decodeContinuously
1 parent a848b22 commit 8cefe65

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

Demo.Server/Pages/Index.razor

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,54 @@
33

44
<h3>BarcodeReader</h3>
55

6-
<h4>Barcode/QR code</h4>
6+
<h5>1D/2D barcode, QR code, Pdf417</h5>
77

88
<button class="btn btn-primary"
99
type="button"
1010
@onclick="(() => ShowScanBarcode = !ShowScanBarcode)">
1111
Scan
1212
</button>
1313

14+
<br />
1415
Pdf417
1516
<input type="checkbox" @bind-value="Pdf417" />
17+
<br />
1618

17-
<input type="text" class="form-control" style="min-width: 100px;"
18-
@bind-value="BarCode"
19-
placeholder="BarCode" />
19+
连续解码/DecodeContinuously
20+
<input type="checkbox" @bind-value="DecodeContinuously" />
21+
22+
<br />
2023

2124
@if (Pdf417)
2225
{
2326
<b>Pdf417:</b>
2427
<br />
2528
<pre>@BarCode</pre>
2629
}
30+
else if (DecodeContinuously)
31+
{
32+
<b>DecodeContinuously:</b>
33+
<br />
34+
<pre>@BarCode</pre>
35+
}
36+
else
37+
{
38+
<input type="text" class="form-control" style="min-width: 100px;"
39+
@bind-value="BarCode"
40+
placeholder="BarCode" />
41+
}
2742

2843
@if (ShowScanBarcode)
2944
{
3045

31-
<BarcodeReader ScanResult="((e) => { BarCode=e; ShowScanBarcode = !ShowScanBarcode; })"
46+
<BarcodeReader ScanResult="ScanResult"
3247
ShowScanBarcode="ShowScanBarcode"
3348
Close="(()=>ShowScanBarcode=!ShowScanBarcode)"
3449
ScanBtnTitle="Scan"
3550
ResetBtnTitle="Reset"
3651
CloseBtnTitle="Close"
3752
Pdf417="Pdf417"
53+
Decodeonce="!DecodeContinuously"
3854
SelectDeviceBtnTitle="Select Device" />
3955

4056
}
@@ -164,6 +180,11 @@ Pdf417
164180
<td><div class="table-cell">false</div></td>
165181
<td><div class="table-cell">Parameter</div></td>
166182
</tr>
183+
<tr>
184+
<td><div class="table-cell">Decodeonce</div></td>
185+
<td><div class="table-cell">true</div></td>
186+
<td><div class="table-cell">Parameter</div></td>
187+
</tr>
167188
<tr>
168189
<td><div class="table-cell">SelectDeviceBtnTitle</div></td>
169190
<td><div class="table-cell">Select device button title</div></td>
@@ -212,6 +233,7 @@ Pdf417
212233
public string? BarCode { get; set; }
213234

214235
public bool Pdf417 { get; set; }
236+
public bool DecodeContinuously { get; set; }
215237

216238
private string message;
217239

@@ -224,6 +246,19 @@ Pdf417
224246

225247
bool ShowCodes;
226248

249+
private void ScanResult(string e)
250+
{
251+
if (!DecodeContinuously)
252+
{
253+
BarCode = e;
254+
ShowScanBarcode = !ShowScanBarcode;
255+
}
256+
else
257+
{
258+
BarCode += e + Environment.NewLine;
259+
}
260+
}
261+
227262
#region Custom
228263

229264
/// <summary>

src/ZXingBlazor/Components/BarcodeReader.razor

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
[Parameter]
100100
public bool Pdf417 { get; set; }
101101

102+
/// <summary>
103+
/// 单次|连续解码,默认单次 / Decode Once or Decode Continuously, default is Once
104+
/// </summary>
105+
[Parameter]
106+
public bool Decodeonce { get; set; } = true;
107+
102108
/// <summary>
103109
/// 选项/ZXingOptions
104110
/// </summary>
@@ -123,9 +129,9 @@
123129
module = await JS.InvokeAsync<IJSObjectReference>("import", "./_content/ZXingBlazor/lib/zxing/zxingjs.js");
124130
if (options == null)
125131
{
126-
options = new ZXingOptions() { Pdf417= Pdf417 };
132+
options = new ZXingOptions() { Pdf417= Pdf417 , Decodeonce = Decodeonce };
127133
}
128-
await module.InvokeVoidAsync("init", true, objRef, barcodeScannerElement, barcodeScannerElement.Id, options);
134+
await module.InvokeVoidAsync("init", objRef, barcodeScannerElement, barcodeScannerElement.Id, options);
129135
}
130136
catch (Exception e)
131137
{

src/ZXingBlazor/Components/ZXingOptions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ namespace ZXingBlazor.Components;
1414
/// </summary>
1515
public class ZXingOptions
1616
{
17-
public bool Pdf417 { get; set; }
17+
/// <summary>
18+
/// decode only Pdf417 format
19+
/// </summary>
20+
public bool Pdf417 { get; set; }
21+
22+
/// <summary>
23+
/// decodeOnce or decodeContinuously
24+
/// </summary>
25+
public bool Decodeonce { get; set; } = true;
1826
}
1927

2028

src/ZXingBlazor/ZXingBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<IsPackable>true</IsPackable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<PackageIcon>logo.png</PackageIcon>
8-
<Version>0.2.4</Version>
8+
<Version>0.2.5</Version>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<Company>Densen Informatica</Company>
1111
<Authors>Alex Chow</Authors>

src/ZXingBlazor/wwwroot/lib/zxing/zxingjs.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import '/_content/ZXingBlazor/lib/zxing/zxing.min.js';
22
var codeReader = null;
33
var id = null;
4-
export function init(autostop, wrapper, element, elementid, options) {
4+
var supportsVibrate = false;
5+
export function init(wrapper, element, elementid, options) {
56
console.log('init' + elementid);
67
id = elementid;
78
let selectedDeviceId;
@@ -10,6 +11,7 @@ export function init(autostop, wrapper, element, elementid, options) {
1011
let startButton = element.querySelector("[data-action=startButton]");
1112
let resetButton = element.querySelector("[data-action=resetButton]");
1213
let closeButton = element.querySelector("[data-action=closeButton]");
14+
supportsVibrate = "vibrate" in navigator;
1315

1416
console.log('init' + startButton.innerHTML);
1517
if (options.pdf417) {
@@ -42,36 +44,44 @@ export function init(autostop, wrapper, element, elementid, options) {
4244
sourceSelectPanel.style.display = 'block'
4345
}
4446

45-
StartScan(autostop);
47+
StartScan();
4648

4749
startButton.addEventListener('click', () => {
4850
StartScan();
4951
})
5052

51-
function StartScan(autostop) {
52-
codeReader.decodeOnceFromVideoDevice(selectedDeviceId, 'video').then((result) => {
53-
console.log(result)
54-
55-
var supportsVibrate = "vibrate" in navigator;
56-
if (supportsVibrate) navigator.vibrate(1000);
57-
58-
if (autostop) {
53+
function StartScan() {
54+
if (options.decodeonce) {
55+
codeReader.decodeOnceFromVideoDevice(selectedDeviceId, 'video').then((result) => {
56+
console.log(result)
57+
if (supportsVibrate) navigator.vibrate(1000);
5958
console.log('autostop');
6059
codeReader.reset();
6160
return wrapper.invokeMethodAsync("GetResult", result.text);
62-
} else {
63-
console.log('None-stop');
64-
codeReader.reset();
65-
wrapper.invokeMethodAsync("GetResult", result.text);
66-
}
61+
}).catch((err) => {
62+
if (err && !(err instanceof ZXing.NotFoundException)) {
63+
console.log(err)
64+
wrapper.invokeMethodAsync("GetError", err + '');
65+
}
66+
})
67+
} else {
68+
codeReader.decodeFromVideoDevice(selectedDeviceId, 'video', (result, err) => {
69+
if (result) {
70+
console.log(result)
71+
if (supportsVibrate) navigator.vibrate(1000);
72+
console.log('None-stop');
73+
wrapper.invokeMethodAsync("GetResult", result.text);
74+
}
75+
if (err && !(err instanceof ZXing.NotFoundException)) {
76+
console.log(err)
77+
wrapper.invokeMethodAsync("GetError", err + '');
78+
}
79+
})
80+
}
6781

68-
}).catch((err) => {
69-
if (err && !(err instanceof ZXing.NotFoundException)) {
70-
console.log(err)
71-
wrapper.invokeMethodAsync("GetError", err + '');
72-
}
73-
})
74-
console.log(`Started continous decode from camera with id ${selectedDeviceId}`)
82+
var x = `decodeContinuously`;
83+
if (options.decodeonce) x = `decodeOnce`;
84+
console.log(`Started ` + x +` decode from camera with id ${selectedDeviceId}`)
7585
}
7686

7787
resetButton.addEventListener('click', () => {

0 commit comments

Comments
 (0)