Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(capture response time): let user choose minimal result count. Fix… #25

Merged
merged 2 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "camcode",
"request": "launch",
"type": "dart"
},
{
"name": "camcode (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "camcode (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
},
{
"name": "example (web)",
"cwd": "example",
"request": "launch",
"type": "dart",
"args": [
"-d",
"chrome",
"--web-port=3000",
"--web-hostname=0.0.0.0",
]
},
{
"name": "example (profile mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "example (release mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ showDialog(
onBarcodeResult: (barcode) {
// do whatever you want
},
minimalResultCount: 2,
),
);
```
Expand Down
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class _CamCodeScannerPageState extends State<CamCodeScannerPage> {
widget.onResult(barcode);
},
controller: _controller,
showDebugFrames: true,
showDebugFrames: false,
minimalResultCount: 1,
),
Positioned(
bottom: 48.0,
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.2.1"
version: "3.0.0"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -169,5 +169,5 @@ packages:
source: hosted
version: "2.1.2"
sdks:
dart: ">=2.17.0-0 <3.0.0"
dart: ">=2.17.0 <3.0.0"
flutter: ">=1.22.6"
21 changes: 19 additions & 2 deletions lib/barcode_results.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
/// Store and count every potential result
/// Once a barcode was identified minimalResultCount times, it is considered as a valid result
class BarcodeResults {
static const minimalResultCount = 2;
/// Set to true if you want to get the first scanned value
/// with no concordance check
bool singleShot;

/// Number of identic scanned value to get before consider getting result
int minimalResultCount;

/// list of barcode results
final Map<String, int> _barcodeResults = {};

BarcodeResults({
this.singleShot = false,
this.minimalResultCount = 2,
});

/// clears all barcode results
void clear() {
_barcodeResults.clear();
Expand All @@ -15,7 +25,14 @@ class BarcodeResults {
int get resultCount => _barcodeResults.values.reduce((a, b) => a + b);

/// Consider that we have a barcode result once enough identic results are found
bool get gotResult =>
bool get gotResult => _containsSingleShotResult || _containesConcordance;

/// return true if singleShot mode is enabled and resultCount is positive
bool get _containsSingleShotResult => singleShot && resultCount > 0;

/// return true if results contains at least minimalResultCount
/// of the same barcode
bool get _containesConcordance =>
resultCount >= 2 &&
_barcodeResults.values.any(
(singleBarcodeCount) => singleBarcodeCount >= minimalResultCount,
Expand Down
5 changes: 5 additions & 0 deletions lib/cam_code_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class CamCodeScanner extends StatefulWidget {
/// shows the current analysing picture
final bool showScannerLine;

/// Number of identic scanned value to get before consider getting result
final int minimalResultCount;

/// Camera barcode scanner widget
/// Params:
/// * showDebugFrames [true|false] - shows the current analysing picture
Expand All @@ -48,6 +51,7 @@ class CamCodeScanner extends StatefulWidget {
this.controller,
this.backgroundColor = Colors.black54,
this.showScannerLine = true,
this.minimalResultCount = 2,
});

@override
Expand Down Expand Up @@ -93,6 +97,7 @@ class _CamCodeScannerState extends State<CamCodeScanner> {
widget.width,
widget.height,
widget.refreshDelayMillis,
widget.minimalResultCount,
],
);

Expand Down
4 changes: 4 additions & 0 deletions lib/camcode_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CamcodeWeb {
arguments[0],
arguments[1],
arguments[2],
arguments[3],
);
case 'releaseResources':
return releaseResources();
Expand Down Expand Up @@ -107,9 +108,12 @@ class CamcodeWeb {
double width,
double height,
int refreshDelayMillis,
int minimalResultCount,
) {
completer = Completer<String>();
_enumerateDevicesCompleter = Completer<List<String>>();
_barcodeResults.singleShot = minimalResultCount == 1;
_barcodeResults.minimalResultCount = minimalResultCount;
_barcodeResults.clear();

// Create a video element which will be provided with stream source
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.16.0"
every_test:
dependency: "direct dev"
description:
name: every_test
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
fake_async:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: camcode
description: A camera barcode scanner for Flutter Web using your favorite Javascript library
version: 3.0.0-SNAPSHOT
version: 3.0.0
repository: https://github.com/PiotrFLEURY/camcode
homepage: https://camcode-59c70.web.app/

Expand All @@ -26,6 +26,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
every_test: ^1.0.0
pedantic: ^1.9.0

environment:
Expand Down
79 changes: 79 additions & 0 deletions test/barcode_results_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:camcode/barcode_results.dart';
import 'package:every_test/every_test.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Expand Down Expand Up @@ -75,4 +76,82 @@ void main() {
expect(mostFrequent, '123456789');
});
});

group('singleShot', () {
test('singleShot enabled', () {
// GIVEN
final barcodeResults = BarcodeResults(singleShot: true);
barcodeResults.add('42424242');

// WHEN
final gotResult = barcodeResults.gotResult;

// THEN
expect(gotResult, true);
});

test('singleShot disabled', () {
// GIVEN
final barcodeResults = BarcodeResults(singleShot: false);
barcodeResults.add('42424242');

// WHEN
final gotResult = barcodeResults.gotResult;

// THEN
expect(gotResult, false);
});

test('singleShot default', () {
// GIVEN
final barcodeResults = BarcodeResults();
barcodeResults.add('42424242');

// WHEN
final gotResult = barcodeResults.gotResult;

// THEN
expect(gotResult, false);
});
});

everyTest(
'minimalResultCount',
of: (params) {
// GIVEN
final _minimalResulCount = params['minimalResultCount'] as int;
final List barcodes = params['barcodes'];
final barcodeResults =
BarcodeResults(minimalResultCount: _minimalResulCount);
barcodes.forEach((it) {
barcodeResults.add(it);
});

// WHEN
return barcodeResults.gotResult;
},
expects: [
// THEN
param({
'minimalResultCount': 1,
'barcodes': ['123456']
}).gives(false),
param({
'minimalResultCount': 2,
'barcodes': ['123456', '123456']
}).gives(true),
param({
'minimalResultCount': 2,
'barcodes': ['123456', '123456', '123456']
}).gives(true),
param({
'minimalResultCount': 2,
'barcodes': ['123456']
}).gives(false),
param({
'minimalResultCount': 4,
'barcodes': ['123456', '123456', '123456']
}).gives(false),
],
);
}