Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit fc10aa1

Browse files
Stop barcode scanner after first set of scanned images received #828
1 parent 6775e5c commit fc10aa1

File tree

8 files changed

+73
-5
lines changed

8 files changed

+73
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
[Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 7.2.0 (2018, October 19)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/79?closed=1)
8+
9+
610
## 7.1.6 (2018, October 12)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/78?closed=1)
812

demo-ng/app/tabs/mlkit/barcodescanning/barcodescanning.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
android:processEveryNthFrame="5"
1111
ios:processEveryNthFrame="10"
1212
[torchOn]="torchOn"
13+
[pause]="pause"
1314
(scanResult)="onBarcodeScanResult($event)">
1415
</MLKitBarcodeScanner>
1516

@@ -36,7 +37,7 @@
3637
<Label height="1" marginBottom="1" borderBottomWidth="1" borderColor="rgba(81, 184, 237, 1)"></Label>
3738
</StackLayout>
3839
</GridLayout>
39-
<Label row="0" col="1" class="text-center c-white" textWrap="true" verticalAlignment="center" text="The scanner has been configured to detect QR codes, EAN 8 and EAN 13. It processes every 5th frame (default 10). These settings can be tweaked in your usage of the plugin."></Label>
40+
<Label row="0" col="1" class="text-center c-white" textWrap="true" verticalAlignment="center" text="The scanner has been configured to detect QR codes, EAN 8 and EAN 13. It processes every 5th frame (default 10). These settings can be tweaked in your usage of the plugin. Oh, and we briefly pause the scanner after every scan, just for show."></Label>
4041
<ListView row="2" col="0" colSpan="3" [items]="barcodes" class="m-t-20" backgroundColor="transparent">
4142
<ng-template let-item="item">
4243
<GridLayout columns="2*, 3*">

demo-ng/app/tabs/mlkit/barcodescanning/barcodescanning.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ export class BarcodeScanningComponent extends AbstractMLKitViewComponent {
1313
format: string;
1414
}>;
1515

16+
pause: boolean = false;
17+
1618
onBarcodeScanResult(event: any): void {
1719
const result: MLKitScanBarcodesOnDeviceResult = event.value;
1820
this.barcodes = result.barcodes;
21+
1922
console.log("this.barcodes: " + JSON.stringify(this.barcodes));
23+
24+
if (this.barcodes.length > 0) {
25+
console.log("pausing the scanner for 3 seconds (to test the 'pause' feature)");
26+
this.pause = true;
27+
setTimeout(() => this.pause = false, 3000)
28+
}
2029
}
2130
}

demo-ng/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"nativescript-angular": "^6.1.0",
2626
"nativescript-camera": "^4.0.2",
2727
"nativescript-imagepicker": "~6.0.4",
28+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.2.0.tgz",
2829
"nativescript-theme-core": "~1.0.4",
2930
"reflect-metadata": "~0.1.10",
3031
"rxjs": "~6.0.0 || >=6.1.0",
@@ -42,4 +43,4 @@
4243
"nativescript-dev-webpack": "^0.15.1",
4344
"typescript": "~2.7.2"
4445
}
45-
}
46+
}

docs/ML_KIT.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,14 @@ The exact details of using the live camera view depend on whether or not you're
118118
You can use any view-related property you like as we're extending `ContentView`.
119119
So things like `class`, `row`, `width`, `horizontalAlignment`, `style` are all valid properties.
120120
121-
Plugin-specific are the optional properties `processEveryNthFrame`, `preferFrontCamera` (default `false`), and `torchOn`, and the optional `scanResult` event.
121+
Plugin-specific are the optional properties `processEveryNthFrame`, `preferFrontCamera` (default `false`), `torchOn`, and `pause`, as well as the optional `scanResult` event.
122122
123123
You can set `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device.
124124
Especially 'Face detection' seems a bit more CPU intensive, but for 'Text recognition' the default is fine.
125125
126+
If you don't destroy the scanner page/modal but instead briefly want to hide it (but keep it alive),
127+
you can pause the scanner with the `pause` property.
128+
126129
> Look at [the demo app](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/tree/master/demo-ng) to see how to wire up that `onTextRecognitionResult` function, and how to wire `torchOn` to a `Switch`.
127130
128131
##### Angular / Vue
@@ -142,6 +145,7 @@ Now you're able to use the registered element in the view:
142145
height="380"
143146
processEveryNthFrame="10"
144147
preferFrontCamera="false"
148+
[pause]="pause"
145149
[torchOn]="torchOn"
146150
(scanResult)="onTextRecognitionResult($event)">
147151
</MLKitTextRecognition>
@@ -161,6 +165,7 @@ Declare a namespace at the top of the embedding page, and use it anywhere on the
161165
height="380"
162166
processEveryNthFrame="3"
163167
preferFrontCamera="false"
168+
pause="{{ pause }}"
164169
scanResult="onTextRecognitionResult" />
165170
166171
</Page>

src/mlkit/mlkit-cameraview-common.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export const torchOnProperty = new Property<MLKitCameraView, boolean>({
1919
valueConverter: booleanConverter
2020
});
2121

22+
export const pauseProperty = new Property<MLKitCameraView, boolean>({
23+
name: "pause",
24+
defaultValue: false,
25+
valueConverter: booleanConverter
26+
});
27+
2228
export abstract class MLKitCameraView extends ContentView {
2329
static scanResultEvent: string = "scanResult";
2430

@@ -27,6 +33,7 @@ export abstract class MLKitCameraView extends ContentView {
2733
protected processEveryNthFrame: number;
2834
protected preferFrontCamera: boolean;
2935
protected torchOn: boolean;
36+
protected pause: boolean;
3037

3138
[processEveryNthFrameProperty.setNative](value: number) {
3239
this.processEveryNthFrame = value;
@@ -41,11 +48,25 @@ export abstract class MLKitCameraView extends ContentView {
4148
this.updateTorch();
4249
}
4350

51+
[pauseProperty.setNative](value: boolean) {
52+
this.pause = value;
53+
this.pause ? this.pauseScanning() : this.resumeScanning();
54+
}
55+
4456
protected updateTorch(): void {
4557
// implemented in concrete classes
4658
};
59+
60+
protected pauseScanning(): void {
61+
// implemented in concrete classes
62+
};
63+
64+
protected resumeScanning(): void {
65+
// implemented in concrete classes
66+
}
4767
}
4868

4969
processEveryNthFrameProperty.register(MLKitCameraView);
5070
preferFrontCameraProperty.register(MLKitCameraView);
5171
torchOnProperty.register(MLKitCameraView);
72+
pauseProperty.register(MLKitCameraView);

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
219219
this.camera.addCallbackBuffer(this.createPreviewBuffer(previewSize));
220220

221221
this.camera.setPreviewDisplay(surfaceHolder);
222-
this.camera.startPreview();
222+
223+
if (!this.pause) {
224+
this.camera.startPreview();
225+
}
223226

224227
}, 500);
225228
}
@@ -232,6 +235,16 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
232235
}
233236
}
234237

238+
protected pauseScanning(): void {
239+
if (this.camera != null) {
240+
this.camera.stopPreview();
241+
}
242+
};
243+
244+
protected resumeScanning(): void {
245+
this.runCamera();
246+
}
247+
235248
protected abstract createDetector(): any;
236249

237250
protected abstract createSuccessListener(): any;

src/mlkit/mlkit-cameraview.ios.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
7878
this.ios.layer.addSublayer(this.previewLayer);
7979
}
8080

81-
this.captureSession.startRunning();
81+
if (!this.pause) {
82+
this.captureSession.startRunning();
83+
}
8284

8385
this.cameraView = TNSMLKitCameraView.alloc().initWithCaptureSession(this.captureSession);
8486
this.cameraView.processEveryXFrames = this.processEveryNthFrame;
@@ -150,6 +152,18 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
150152
}
151153
}
152154

155+
protected pauseScanning(): void {
156+
if (this.captureSession && this.captureSession.running) {
157+
this.captureSession.stopRunning();
158+
}
159+
};
160+
161+
protected resumeScanning(): void {
162+
if (this.captureSession && !this.captureSession.running) {
163+
this.captureSession.startRunning();
164+
}
165+
}
166+
153167
abstract createDetector(): any;
154168

155169
abstract createSuccessListener(): any;

0 commit comments

Comments
 (0)