Skip to content

Commit 98c2ebc

Browse files
committed
fix: unify camera permission handling, detection background handling, detection sig, support processEveryNthFrame, onTorch handling
closes: #13 closes: #20 closes: #21 closes: #22 closes: #23 closes: #24
1 parent 31d601d commit 98c2ebc

File tree

23 files changed

+686
-601
lines changed

23 files changed

+686
-601
lines changed

apps/demo/src/plugin-demos/mlkit-core.ts

+59-56
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,72 @@ import { ObjectResult } from '@nativescript/mlkit-object-detection';
88
import { PoseResult } from '@nativescript/mlkit-pose-detection';
99
import { TextResult } from '@nativescript/mlkit-text-recognition';
1010
export function navigatingTo(args: EventData) {
11-
const page = <Page>args.object;
12-
page.bindingContext = new DemoModel();
11+
const page = <Page>args.object;
12+
page.bindingContext = new DemoModel();
1313
}
1414

1515
export class DemoModel extends DemoSharedMlkitCore {
16-
camera: MLKitView;
17-
detectorType = "all";
18-
isPaused = false;
19-
onLoaded(args) {
20-
this.camera = args.object;
21-
}
16+
camera: MLKitView;
17+
detectorType = DetectionType.Barcode;
18+
isPaused = false;
19+
torchOn = false;
20+
onLoaded(args) {
21+
this.camera = args.object;
22+
this.set('isPaused', this.camera.pause);
23+
this.set('torchOn', this.camera.torchOn);
24+
}
2225

23-
onDetection(event: DetectionEvent) {
24-
console.log('onDetection', event.data, event.type);
25-
}
26+
onDetection(event: DetectionEvent) {
27+
console.log('onDetection', event.data, event.type);
28+
if (event.type === DetectionType.Barcode) {
29+
const first = event.data[0] as BarcodeResult;
30+
console.log('bounds', first.bounds);
31+
}
32+
}
2633

27-
toggleCamera() {
28-
this.camera.toggleCamera();
29-
}
34+
toggleCamera() {
35+
this.camera.toggleCamera();
36+
}
3037

31-
requestPermission() {
32-
this.camera.requestCameraPermission();
33-
}
38+
toggleTorch() {
39+
this.camera.torchOn = !this.camera.torchOn
40+
this.set('torchOn', this.camera.torchOn);
41+
}
3442

35-
changeType(args) {
36-
Dialogs.action('Change Detector Type', 'Cancel', [
37-
'all',
38-
'barcode',
39-
'digitalInk (unsupport atm)',
40-
'face',
41-
'image',
42-
'object',
43-
'pose',
44-
'text',
45-
'none'
46-
]).then(value => {
47-
if (value === 'Cancel') { return }
48-
if (value.indexOf('digitalInk') > -1) {
49-
Dialogs.alert('digitalInk is currently unsupported')
50-
this.set('detectorType', 'none');
51-
} else {
52-
this.set('detectorType', value);
53-
}
54-
})
55-
}
43+
requestPermission() {
44+
this.camera.requestCameraPermission();
45+
}
5646

57-
togglePause(args) {
58-
this.camera.pause = !this.camera.pause;
59-
this.set('isPaused', this.camera.pause);
60-
}
47+
changeType(args) {
48+
Dialogs.action('Change Detector Type', 'Cancel', ['all', 'barcode', 'digitalInk (unsupport atm)', 'face', 'image', 'object', 'pose', 'text', 'none']).then((value) => {
49+
if (value === 'Cancel') {
50+
return;
51+
}
52+
if (value.indexOf('digitalInk') > -1) {
53+
Dialogs.alert('digitalInk is currently unsupported');
54+
this.set('detectorType', 'none');
55+
} else {
56+
this.set('detectorType', value);
57+
}
58+
});
59+
}
6160

62-
async processStill(args) {
63-
try {
64-
const src = await ImageSource.fromUrl('https://www.jqueryscript.net/images/jQuery-Plugin-To-Generate-International-Article-Number-Barcode-EAN13.jpg');
65-
66-
console.log(src.android)
67-
const result = await detectWithStillImage(src, {
68-
detectorType: DetectionType.Barcode
69-
});
70-
console.log('processStill', result.barcode[0]);
71-
} catch (e) {
72-
console.log(e);
73-
}
74-
}
75-
}
61+
togglePause(args) {
62+
this.camera.pause = !this.camera.pause;
63+
this.set('isPaused', this.camera.pause);
64+
}
65+
66+
async processStill(args) {
67+
try {
68+
const src = await ImageSource.fromUrl('https://www.jqueryscript.net/images/jQuery-Plugin-To-Generate-International-Article-Number-Barcode-EAN13.jpg');
7669

70+
console.log(src.android);
71+
const result = await detectWithStillImage(src, {
72+
detectorType: DetectionType.Barcode,
73+
});
74+
console.log('processStill', result.barcode[0]);
75+
} catch (e) {
76+
console.log(e);
77+
}
78+
}
79+
}

apps/demo/src/plugin-demos/mlkit-core.xml

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
<ActionBar title="mlkit-core" icon="" class="action-bar">
44
</ActionBar>
55
</Page.actionBar>
6-
<GridLayout rows="*,auto,auto,auto,auto,auto,auto,auto" height="100%">
7-
<ui:MLKitView pause="false" height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" detection="{{ onDetection }}"/>
6+
<GridLayout rows="*,auto,auto,auto,auto,auto,auto,auto,auto,auto" height="100%">
7+
<ui:MLKitView pause="true" height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" detection="{{ onDetection }}"/>
88
<Button row="1" height="40" text="Toggle Camera" tap="{{ toggleCamera }}" />
9-
<Button row="2" height="40" text="Request Camera Permission" tap="{{ requestPermission }}" />
10-
<Label row="3" text="{{'Detecting ' + detectorType }}"/>
11-
<Button row="4" height="40" text="Change Detector Type" tap="{{ changeType }}" />
12-
<Label row="5" text="{{'isPaused : ' + isPaused }}"/>
13-
<Button row="6" height="40" text="Toggle Pause" tap="{{ togglePause }}" />
14-
<Button row="7" height="40" text="Process Still imAGE" tap="{{ processStill }}" />
9+
<Label color="red" row="2" text="{{'TorchOn ' + torchOn }}"/>
10+
<Button row="3" height="40" text="Toggle Torch" tap="{{ toggleTorch }}" />
11+
<Button row="4" height="40" text="Request Camera Permission" tap="{{ requestPermission }}" />
12+
<Label row="5" text="{{'Detecting ' + detectorType }}"/>
13+
<Button row="6" height="40" text="Change Detector Type" tap="{{ changeType }}" />
14+
<Label row="7" text="{{'isPaused : ' + isPaused }}"/>
15+
<Button row="8" height="40" text="Toggle Pause" tap="{{ togglePause }}" />
16+
<Button row="9" height="40" text="Process Still imAGE" tap="{{ processStill }}" />
1517
</GridLayout>
1618
</Page>

packages/mlkit-barcode-scanning/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-barcode-scanning",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"description": "NativeScript MLKit Barcode Scanner module",
55
"main": "index",
66
"typings": "index.d.ts",
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies {
2-
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.3'
2+
implementation 'io.github.triniwiz.fancycamera:barcodeScanning:1.0.0-alpha.4'
33
implementation 'com.google.mlkit:barcode-scanning:17.0.1'
44
}

packages/mlkit-core/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
7777
import { BarcodeResult } from '@nativescript/mlkit-barcode-scanning';
7878
onDetection(event: DetectionEvent){
7979
if(event.type === DetectionType.Barcode){
80-
const barcode: BarcodeResult = data;
80+
const barcode: BarcodeResult[] = event.data;
8181
}
8282
}
8383
```
@@ -93,7 +93,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
9393
import { FaceResult } from '@nativescript/mlkit-face-detection';
9494
onDetection(event: DetectionEvent){
9595
if(event.type === DetectionType.Face){
96-
const faces: FaceResult[] = data;
96+
const faces: FaceResult[] = event.data;
9797
}
9898
}
9999
```
@@ -110,7 +110,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
110110
import { ImageLabelingResult } from '@nativescript/mlkit-image-labeling';
111111
onDetection(event: DetectionEvent){
112112
if(event.type === DetectionType.Image){
113-
const labels: ImageLabelingResult[] = data;
113+
const labels: ImageLabelingResult[] = event.data;
114114
}
115115
}
116116
```
@@ -127,7 +127,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
127127
import { ObjectResult } from '@nativescript/mlkit-object-detection'
128128
onDetection(event: DetectionEvent){
129129
if(event.type === DetectionType.Object){
130-
const objects: ObjectResult[] = data;
130+
const objects: ObjectResult[] = event.data;
131131
}
132132
}
133133
```
@@ -143,7 +143,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
143143
import { PoseResult } from '@nativescript/mlkit-pose-detection';
144144
onDetection(event: DetectionEvent){
145145
if(event.type === DetectionType.Pose){
146-
const poses: PoseResult[] = data;
146+
const poses: PoseResult = event.data;
147147
}
148148
}
149149
```
@@ -160,7 +160,7 @@ import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
160160
import { TextResult } from '@nativescript/mlkit-text-recognition';
161161
onDetection(event: DetectionEvent){
162162
if(event.type === DetectionType.Text){
163-
const text: TextResult[] = data;
163+
const text: TextResult = event.data;
164164
}
165165
}
166166
```

packages/mlkit-core/common.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export enum FaceDetectionPerformanceMode {
4545

4646
@CSSType('MLKitView')
4747
export class MLKitViewBase extends ContainerView {
48-
public static detectionEvent = 'detection';
48+
static detectionEvent = 'detection';
4949
cameraPosition: CameraPosition;
5050
detectionType: DetectionType;
5151
barcodeFormats: BarcodeFormats[];
@@ -57,6 +57,7 @@ export class MLKitViewBase extends ContainerView {
5757
objectDetectionClassify: boolean;
5858
torchOn: boolean;
5959
pause: boolean;
60+
processEveryNthFrame: number;
6061
}
6162

6263
export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition>({
@@ -140,11 +141,17 @@ export const torchOnProperty = new Property<MLKitViewBase, boolean>({
140141
torchOnProperty.register(MLKitViewBase);
141142

142143

143-
144144
export const pauseProperty = new Property<MLKitViewBase, boolean>({
145145
name: 'pause',
146146
defaultValue: false,
147147
valueConverter: booleanConverter
148148
});
149149

150150
pauseProperty.register(MLKitViewBase);
151+
152+
export const processEveryNthFrameProperty = new Property<MLKitViewBase, number>({
153+
name: 'processEveryNthFrame',
154+
defaultValue: 0
155+
});
156+
157+
processEveryNthFrameProperty.register(MLKitViewBase);

packages/mlkit-core/index.android.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, torchOnProperty } from "./common";
1+
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, processEveryNthFrameProperty, torchOnProperty } from "./common";
22
import { Application, Device, Utils, AndroidActivityRequestPermissionsEventData, ImageSource } from '@nativescript/core';
33
import lazy from '@nativescript/core/utils/lazy';
44
import { StillImageDetectionOptions } from ".";
@@ -149,6 +149,12 @@ export class MLKitView extends MLKitViewBase {
149149
this.#setListeners();
150150
}
151151

152+
[processEveryNthFrameProperty.setNative](value){
153+
if(this.#camera){
154+
this.#camera.setProcessEveryNthFrame(value);
155+
}
156+
}
157+
152158
initNativeView() {
153159
super.initNativeView();
154160
this.#setListeners();
@@ -319,6 +325,7 @@ export class MLKitView extends MLKitViewBase {
319325
}
320326
}
321327

328+
322329
[barcodeFormatsProperty.setNative](value: BarcodeFormats[]) {
323330
if (!BARCODE_SCANNER_SUPPORTED()) {
324331
return;

packages/mlkit-core/index.d.ts

+37-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1-
import { MLKitViewBase, DetectionType, BarcodeFormats } from "./common";
1+
import { MLKitViewBase, DetectionType, BarcodeFormats } from './common';
22
import { EventData } from '@nativescript/core';
33

4-
54
export interface StillImageDetectionOptions {
6-
detectorType: DetectionType
7-
barcodeScanning?: {
8-
barcodeFormat?: [BarcodeFormats]
9-
}
10-
faceDetection?: {
11-
faceTracking?: boolean
12-
minimumFaceSize: ?number
13-
detectionMode?: "fast" | "accurate"
14-
landmarkMode?: "all" | "none"
15-
contourMode?: "all" | "none"
16-
classificationMode?: "all" | "none"
17-
}
18-
imageLabeling?: {
19-
confidenceThreshold?: number
20-
}
21-
objectDetection?: {
22-
multiple: boolean;
23-
classification: boolean;
24-
}
25-
selfieSegmentation?: {
26-
enableRawSizeMask?: boolean;
27-
smoothingRatio?: number;
28-
}
5+
detectorType: DetectionType;
6+
barcodeScanning?: {
7+
barcodeFormat?: [BarcodeFormats];
8+
};
9+
faceDetection?: {
10+
faceTracking?: boolean;
11+
minimumFaceSize: ?number;
12+
detectionMode?: 'fast' | 'accurate';
13+
landmarkMode?: 'all' | 'none';
14+
contourMode?: 'all' | 'none';
15+
classificationMode?: 'all' | 'none';
16+
};
17+
imageLabeling?: {
18+
confidenceThreshold?: number;
19+
};
20+
objectDetection?: {
21+
multiple: boolean;
22+
classification: boolean;
23+
};
24+
selfieSegmentation?: {
25+
enableRawSizeMask?: boolean;
26+
smoothingRatio?: number;
27+
};
2928
}
3029

3130
export interface DetectionEvent extends EventData {
32-
data: { [key: string]: any };
33-
type: DetectionType
31+
data: { [key: string]: any } | Array<{ [key: string]: any }>;
32+
type: DetectionType;
3433
}
34+
35+
3536
export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty as imageLablerConfidenceThresholdProperty, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from './common';
3637
export declare class MLKitView extends MLKitViewBase {
37-
static isAvailable(): boolean;
38-
static detectionEvent: string;
39-
stopPreview(): void;
40-
toggleCamera(): void;
41-
startPreview(): void;
42-
requestCameraPermission(): Promise<void>;
43-
hasCameraPermission(): boolean;
44-
on(event: 'detection', callback: (args: DetectionEvent) => void, thisArg?: any);
38+
static isAvailable(): boolean;
39+
static detectionEvent: string;
40+
stopPreview(): void;
41+
toggleCamera(): void;
42+
startPreview(): void;
43+
requestCameraPermission(): Promise<void>;
44+
hasCameraPermission(): boolean;
45+
on(event: 'detection', callback: (args: DetectionEvent) => void, thisArg?: any);
4546
}
4647

47-
export function detectWithStillImage(image: any, options?: StillImageDetectionOptions): Promise<{ [key: string]: any }>
48+
export function detectWithStillImage(image: any, options?: StillImageDetectionOptions): Promise<{ [key: string]: any }>;

0 commit comments

Comments
 (0)