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

Commit b92431e

Browse files
[Android] Flashlight doesn't work with Barcode scanner #852
1 parent e29cef2 commit b92431e

13 files changed

+105
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PropertyChangeData } from "tns-core-modules/data/observable";
2+
3+
export abstract class AbstractMLKitViewComponent {
4+
torchOn: boolean = false;
5+
6+
toggleTorch(args: PropertyChangeData): void {
7+
if (args.value !== null && args.value !== this.torchOn) {
8+
this.torchOn = args.value;
9+
}
10+
}
11+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
formats="QR_CODE, EAN_8, EAN_13"
1010
android:processEveryNthFrame="5"
1111
ios:processEveryNthFrame="10"
12+
[torchOn]="torchOn"
1213
(scanResult)="onBarcodeScanResult($event)">
1314
</MLKitBarcodeScanner>
1415

@@ -45,4 +46,10 @@
4546
</ng-template>
4647
</ListView>
4748
</GridLayout>
49+
50+
<GridLayout rows="auto" columns="auto, auto" horizontalAlignment="right" class="m-t-4 m-r-8">
51+
<Label col="0" text="Torch" class="c-white" [class.disabled]="!torchOn"></Label>
52+
<Switch col="1" [checked]="torchOn" (checkedChange)="toggleTorch($event)"></Switch>
53+
</GridLayout>
54+
4855
</GridLayout>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { Component } from "@angular/core";
22
import { MLKitScanBarcodesOnDeviceResult } from "nativescript-plugin-firebase/mlkit/barcodescanning";
3+
import { AbstractMLKitViewComponent } from "~/tabs/mlkit/abstract.mlkitview.component";
34

45
@Component({
56
selector: "mlkit-barcodescanning",
67
moduleId: module.id,
78
templateUrl: "./barcodescanning.component.html",
89
})
9-
export class BarcodeScanningComponent {
10+
export class BarcodeScanningComponent extends AbstractMLKitViewComponent {
1011
barcodes: Array<{
1112
value: string;
1213
format: string;
1314
}>;
1415

16+
constructor() {
17+
super();
18+
// let's start with the torch on, just for show
19+
this.torchOn = true;
20+
}
21+
1522
onBarcodeScanResult(event: any): void {
1623
const result: MLKitScanBarcodesOnDeviceResult = event.value;
1724
this.barcodes = result.barcodes;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
enableFaceTracking="true"
1212
minimumFaceSize="0.2"
1313
modeType="accurate"
14+
[torchOn]="torchOn"
1415
(scanResult)="onFaceDetectionResult($event)">
1516
</MLKitFaceDetection>
1617

@@ -59,4 +60,10 @@
5960
</GridLayout>
6061
</ng-template>
6162
</ListView>
63+
64+
<GridLayout rows="auto" columns="auto, auto" horizontalAlignment="right" class="m-t-4 m-r-8">
65+
<Label col="0" text="Torch" class="c-white" [class.disabled]="!torchOn"></Label>
66+
<Switch col="1" [checked]="torchOn" (checkedChange)="toggleTorch($event)"></Switch>
67+
</GridLayout>
68+
6269
</GridLayout>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Component } from "@angular/core";
22
import { MLKitDetectFacesOnDeviceResult, MLKitDetectFacesResultFace } from "nativescript-plugin-firebase/mlkit/facedetection";
3+
import { AbstractMLKitViewComponent } from "~/tabs/mlkit/abstract.mlkitview.component";
34

45
@Component({
56
selector: "mlkit-facedetection",
67
moduleId: module.id,
78
templateUrl: "./facedetection.component.html",
89
})
9-
export class FaceDetectionComponent {
10+
export class FaceDetectionComponent extends AbstractMLKitViewComponent {
1011
faces: Array<MLKitDetectFacesResultFace>;
1112

1213
mlKitAllOK: string;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@
4242
</ng-template>
4343
</ListView>
4444
</GridLayout>
45+
46+
<GridLayout rows="auto" columns="auto, auto" horizontalAlignment="right" class="m-t-4 m-r-8">
47+
<Label col="0" text="Torch" class="c-white" [class.disabled]="!torchOn"></Label>
48+
<Switch col="1" [checked]="torchOn" (checkedChange)="toggleTorch($event)"></Switch>
49+
</GridLayout>
50+
4551
</GridLayout>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Component } from "@angular/core";
22
import { MLKitImageLabelingOnDeviceResult } from "nativescript-plugin-firebase/mlkit/imagelabeling";
3+
import { AbstractMLKitViewComponent } from "~/tabs/mlkit/abstract.mlkitview.component";
34

45
@Component({
56
selector: "mlkit-imagelabeling",
67
moduleId: module.id,
78
templateUrl: "./imagelabeling.component.html",
89
})
9-
export class ImageLabelingComponent {
10+
export class ImageLabelingComponent extends AbstractMLKitViewComponent {
1011
labels: Array<{
1112
text: string;
1213
confidence: number;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<MLKitTextRecognition
77
width="100%"
88
height="100%"
9+
[torchOn]="torchOn"
910
(scanResult)="onTextRecognitionResult($event)">
1011
</MLKitTextRecognition>
1112

@@ -38,4 +39,10 @@
3839
</ng-template>
3940
</ListView>
4041
</GridLayout>
42+
43+
<GridLayout rows="auto" columns="auto, auto" horizontalAlignment="right" class="m-t-4 m-r-8">
44+
<Label col="0" text="Torch" class="c-white" [class.disabled]="!torchOn"></Label>
45+
<Switch col="1" [checked]="torchOn" (checkedChange)="toggleTorch($event)"></Switch>
46+
</GridLayout>
47+
4148
</GridLayout>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {
33
MLKitRecognizeTextResult,
44
MLKitRecognizeTextResultBlock
55
} from "nativescript-plugin-firebase/mlkit/textrecognition";
6+
import { AbstractMLKitViewComponent } from "~/tabs/mlkit/abstract.mlkitview.component";
67

78
@Component({
89
selector: "mlkit-textrecognition",
910
moduleId: module.id,
1011
templateUrl: "./textrecognition.component.html",
1112
})
12-
export class TextRecognitionComponent {
13+
export class TextRecognitionComponent extends AbstractMLKitViewComponent {
1314
blocks: Array<MLKitRecognizeTextResultBlock>;
1415

1516
onTextRecognitionResult(scanResult: any): void {

docs/ML_KIT.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ The exact details of using the live camera view depend on whether or not you're
120120
You can use any view-related property you like as we're extending `ContentView`.
121121
So things like `class`, `row`, `width`, `horizontalAlignment`, `style` are all valid properties.
122122
123-
Plugin-specific are the optional property `processEveryNthFrame` and optional event `scanResult`.
123+
Plugin-specific are the optional properties `processEveryNthFrame` and `torchOn`, and optional event `scanResult`.
124124
You can `processEveryNthFrame` set to a lower value than the default (5) to put less strain on the device.
125125
Especially 'Face detection' seems a bit more CPU intensive, but for 'Text recognition' the default is fine.
126126
127-
> 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.
127+
> 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`.
128128
129129
##### Angular / Vue
130130
Register a custom element like so in the component/module:
@@ -142,6 +142,7 @@ Now you're able to use the registered element in the view:
142142
width="260"
143143
height="380"
144144
processEveryNthFrame="10"
145+
[torchOn]="torchOn"
145146
(scanResult)="onTextRecognitionResult($event)">
146147
</MLKitTextRecognition>
147148
```
@@ -202,6 +203,7 @@ registerElement("MLKitFaceDetection", () => require("nativescript-plugin-firebas
202203
detectionMode="accurate"
203204
enableFaceTracking="true"
204205
minimumFaceSize="0.2"
206+
[torchOn]="torchOn"
205207
(scanResult)="onFaceDetectionResult($event)">
206208
</MLKitFaceDetection>
207209
```
@@ -238,6 +240,7 @@ registerElement("MLKitBarcodeScanner", () => require("nativescript-plugin-fireba
238240
width="260"
239241
height="380"
240242
formats="QR_CODE, EAN_8, EAN_13"
243+
[torchOn]="torchOn"
241244
(scanResult)="onBarcodeScanningResult($event)">
242245
</MLKitBarcodeScanner>
243246
```
@@ -289,6 +292,7 @@ registerElement("MLKitImageLabeling", () => require("nativescript-plugin-firebas
289292
width="260"
290293
height="380"
291294
confidenceThreshold="0.6"
295+
[torchOn]="torchOn"
292296
(scanResult)="onImageLabelingResult($event)">
293297
</MLKitImageLabeling>
294298
```

0 commit comments

Comments
 (0)