Skip to content

Commit 16642aa

Browse files
committed
fix: detection event
fixes #5
1 parent 26cbcd9 commit 16642aa

File tree

22 files changed

+180
-90
lines changed

22 files changed

+180
-90
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Observable, EventData, Page, Dialogs } from '@nativescript/core';
22
import { DemoSharedMlkitCore } from '@demo/shared';
3-
import { DetectionType, MLKitView } from '@nativescript/mlkit-core';
3+
import { DetectionType, MLKitView, DetectionEvent } from '@nativescript/mlkit-core';
44
import { BarcodeResult } from '@nativescript/mlkit-barcode-scanning';
55
import { FaceResult } from '@nativescript/mlkit-face-detection';
66
import { ImageLabelingResult } from '@nativescript/mlkit-image-labeling';
@@ -20,8 +20,8 @@ export class DemoModel extends DemoSharedMlkitCore {
2020
this.camera = args.object;
2121
}
2222

23-
onDetection(data, type: DetectionType) {
24-
console.log('onDetection', data, type);
23+
onDetection(event: DetectionEvent) {
24+
console.log('onDetection', event.data, event.type);
2525
}
2626

2727
toggleCamera() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</ActionBar>
55
</Page.actionBar>
66
<GridLayout rows="*,auto,auto,auto,auto" height="100%">
7-
<ui:MLKitView height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" onDetection="{{ onDetection }}"/>
7+
<ui:MLKitView height="100%" rowSpan="3" loaded="{{ onLoaded }}" cameraPosition="back" detectionType="{{ detectorType }}" detection="{{ onDetection }}"/>
88
<Button row="1" height="40" text="Toggle Camera" tap="{{ toggleCamera }}" />
99
<Button row="2" height="40" text="Request Camera Permission" tap="{{ requestPermission }}" />
1010
<Label row="3" text="{{'Detecting ' + detectorType }}"/>

packages/mlkit-barcode-scanning/index.d.ts renamed to packages/mlkit-barcode-scanning/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ export enum ValueType {
134134
Unknown = "unknown"
135135
}
136136

137+
export enum BarcodeFormats {
138+
ALL = 'all',
139+
CODE_128 = 'code_128',
140+
CODE_39 = 'code_39',
141+
CODE_93 = 'code_93',
142+
CODABAR = 'codabar',
143+
DATA_MATRIX = 'data_matrix',
144+
EAN_13 = 'ean_13',
145+
EAN_8 = 'ean_8',
146+
ITF = 'itf',
147+
QR_CODE = 'qr_code',
148+
UPC_A = 'upc_a',
149+
UPC_E = 'upc_e',
150+
PDF417 = 'pdf417',
151+
AZTEC = 'aztec',
152+
UNKOWN = 'unknown'
153+
}
154+
155+
137156

138157
export interface BarcodeResult {
139158
calendarEvent?: CalenderEvent
@@ -143,7 +162,7 @@ export interface BarcodeResult {
143162
displayValue?: string
144163
driverLicense?: DriverLicense
145164
email?: Email
146-
format: BarcodeScanner.BarcodeFormat
165+
format: BarcodeFormats
147166
geoPoint?: GeoPoint
148167
phone?: Phone
149168
rawBytes?: any[]

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.0",
3+
"version": "1.0.0-alpha.1",
44
"description": "NativeScript MLKit Barcode Scanner module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-core/README.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ns plugin add @nativescript/mlkit-core
1515
<ui:MLKitView
1616
cameraPosition="back"
1717
detectionType="all"
18-
onDetection="onDetection"
18+
detection="onDetection"
1919
/>
2020
```
2121

@@ -37,11 +37,11 @@ import { MLKitModule } from '@nativescript/mlkit-core/angular';
3737
```
3838

3939

40-
```xml
40+
```html
4141
<MLKitView
4242
cameraPosition="back"
4343
detectionType="all"
44-
onDetection="onDetection"
44+
(detection)="onDetection"
4545
></MLKitView>
4646
```
4747

@@ -58,7 +58,7 @@ Vue.use(MLKit)
5858
<MLKitView
5959
cameraPosition="back"
6060
detectionType="all"
61-
onDetection="onDetection"
61+
@detection="onDetection"
6262
></MLKitView>
6363
```
6464

@@ -73,10 +73,10 @@ ns plugin add @nativescript/mlkit-barcode-scanning
7373
```
7474

7575
```ts
76-
import { DetectionType } from '@nativescript/mlkit-core';
76+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
7777
import { BarcodeResult } from '@nativescript/mlkit-barcode-scanning';
78-
onDetection(data, type){
79-
if(type === DetectionType.Barcode){
78+
onDetection(event: DetectionEvent){
79+
if(event.type === DetectionType.Barcode){
8080
const barcode: BarcodeResult = data;
8181
}
8282
}
@@ -89,10 +89,10 @@ ns plugin add @nativescript/mlkit-face-detection
8989
```
9090

9191
```ts
92-
import { DetectionType } from '@nativescript/mlkit-core';
92+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
9393
import { FaceResult } from '@nativescript/mlkit-face-detection';
94-
onDetection(data, type){
95-
if(type === DetectionType.Face){
94+
onDetection(event: DetectionEvent){
95+
if(event.type === DetectionType.Face){
9696
const faces: FaceResult[] = data;
9797
}
9898
}
@@ -106,10 +106,10 @@ ns plugin add @nativescript/mlkit-image-labeling
106106
```
107107

108108
```ts
109-
import { DetectionType } from '@nativescript/mlkit-core';
109+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
110110
import { ImageLabelingResult } from '@nativescript/mlkit-image-labeling';
111-
onDetection(data, type){
112-
if(type === DetectionType.Image){
111+
onDetection(event: DetectionEvent){
112+
if(event.type === DetectionType.Image){
113113
const labels: ImageLabelingResult[] = data;
114114
}
115115
}
@@ -123,10 +123,10 @@ ns plugin add @nativescript/mlkit-object-detection
123123
```
124124

125125
```ts
126-
import { DetectionType } from '@nativescript/mlkit-core';
126+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
127127
import { ObjectResult } from '@nativescript/mlkit-object-detection'
128-
onDetection(data, type){
129-
if(type === DetectionType.Object){
128+
onDetection(event: DetectionEvent){
129+
if(event.type === DetectionType.Object){
130130
const objects: ObjectResult[] = data;
131131
}
132132
}
@@ -139,10 +139,10 @@ ns plugin add @nativescript/mlkit-pose-detection
139139
```
140140

141141
```ts
142-
import { DetectionType } from '@nativescript/mlkit-core';
142+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
143143
import { PoseResult } from '@nativescript/mlkit-pose-detection';
144-
onDetection(data, type){
145-
if(type === DetectionType.Pose){
144+
onDetection(event: DetectionEvent){
145+
if(event.type === DetectionType.Pose){
146146
const poses: PoseResult[] = data;
147147
}
148148
}
@@ -156,10 +156,10 @@ ns plugin add @nativescript/mlkit-text-recognition
156156
```
157157

158158
```ts
159-
import { DetectionType } from '@nativescript/mlkit-core';
159+
import { DetectionType, DetectionEvent } from '@nativescript/mlkit-core';
160160
import { TextResult } from '@nativescript/mlkit-text-recognition';
161-
onDetection(data, type){
162-
if(type === DetectionType.Text){
161+
onDetection(event: DetectionEvent){
162+
if(event.type === DetectionType.Text){
163163
const text: TextResult[] = data;
164164
}
165165
}

packages/mlkit-core/common.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export enum FaceDetectionPerformanceMode {
4444

4545
@CSSType('MLKitView')
4646
export class MLKitViewBase extends ContainerView {
47+
public static detectionEvent = 'detection';
4748
cameraPosition: CameraPosition;
4849
detectionType: DetectionType;
4950
barcodeFormats: BarcodeFormats[];
@@ -53,16 +54,8 @@ export class MLKitViewBase extends ContainerView {
5354
imageLabelerConfidenceThreshold: number;
5455
objectDetectionMultiple: boolean;
5556
objectDetectionClassify: boolean;
56-
onDetection: (data: {
57-
[key: string]: any;
58-
}, type: DetectionType) => void;
5957
}
6058

61-
export const onDetectionProperty = new Property<MLKitViewBase, (data: { [key: string]: any }) => void>({
62-
name: 'onDetection'
63-
});
64-
onDetectionProperty.register(MLKitViewBase);
65-
6659
export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition>({
6760
name: 'cameraPosition',
6861
defaultValue: CameraPosition.BACK

packages/mlkit-core/index.android.ts

+79-11
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, onDetectionProperty } from "./common";
1+
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from "./common";
22
import { Application, Device, Utils, AndroidActivityRequestPermissionsEventData } from '@nativescript/core';
33
import lazy from '@nativescript/core/utils/lazy';
44

@@ -117,17 +117,31 @@ export class MLKitView extends MLKitViewBase {
117117
type = DetectorType_None();
118118
break;
119119
}
120-
121120
this.#camera.setDetectorType(type);
121+
this.#setListeners();
122+
}
123+
124+
initNativeView() {
125+
super.initNativeView();
126+
this.#setListeners();
122127
}
123128

124-
[onDetectionProperty.setNative](value) {
129+
#setListeners() {
125130
const ref = new WeakRef(this);
126131
if (!this.#onTextListener && (this.detectionType === DetectionType.Text || this.detectionType === DetectionType.All)) {
127132
this.#onTextListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
128133
onSuccess(param0: string) {
134+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
135+
if (!hasListener) {
136+
return;
137+
}
129138
try {
130-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Text);
139+
ref?.get?.().notify?.({
140+
eventName: MLKitView.detectionEvent,
141+
object: ref?.get?.(),
142+
data: JSON.parse(param0),
143+
type: DetectionType.Text
144+
});
131145
} catch (e) { }
132146
},
133147
onError(param0: string, param1: java.lang.Exception) {
@@ -140,8 +154,17 @@ export class MLKitView extends MLKitViewBase {
140154
if (!this.#onBarcodeListener && (this.detectionType.includes(DetectionType.Barcode) || this.detectionType.includes(DetectionType.All))) {
141155
this.#onBarcodeListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
142156
onSuccess(param0: string) {
157+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
158+
if (!hasListener) {
159+
return;
160+
}
143161
try {
144-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Barcode);
162+
ref?.get?.().notify?.({
163+
eventName: MLKitView.detectionEvent,
164+
object: ref?.get?.(),
165+
data: JSON.parse(param0),
166+
type: DetectionType.Barcode
167+
});
145168
} catch (e) { }
146169
},
147170
onError(param0: string, param1: java.lang.Exception) {
@@ -155,8 +178,17 @@ export class MLKitView extends MLKitViewBase {
155178
if (!this.#onDigitalInkListener && (this.detectionType === DetectionType.DigitalInk || this.detectionType === DetectionType.All)) {
156179
/* this.#onDigitalInkListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
157180
onSuccess(param0: string) {
181+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
182+
if(!hasListener){
183+
return;
184+
}
158185
try {
159-
ref?.get?.().onDetection?.(JSON.parse(param0));
186+
ref?.get?.().notify?.({
187+
eventName: MLKitView.detectionEvent,
188+
object: ref?.get?.(),
189+
data: JSON.parse(param0),
190+
type: DetectionType.DigitalInk
191+
});
160192
} catch (e) { }
161193
},
162194
onError(param0: string, param1: java.lang.Exception) {
@@ -169,8 +201,17 @@ export class MLKitView extends MLKitViewBase {
169201
this.#faceDetectionOptions = new io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options();
170202
this.#onFaceListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
171203
onSuccess(param0: string) {
204+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
205+
if (!hasListener) {
206+
return;
207+
}
172208
try {
173-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Face);
209+
ref?.get?.().notify?.({
210+
eventName: MLKitView.detectionEvent,
211+
object: ref?.get?.(),
212+
data: JSON.parse(param0),
213+
type: DetectionType.Face
214+
});
174215
} catch (e) { }
175216
},
176217
onError(param0: string, param1: java.lang.Exception) {
@@ -183,8 +224,17 @@ export class MLKitView extends MLKitViewBase {
183224
if (!this.#onImageListener && (this.detectionType === DetectionType.Image || this.detectionType === DetectionType.All)) {
184225
this.#onImageListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
185226
onSuccess(param0: string) {
227+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
228+
if (!hasListener) {
229+
return;
230+
}
186231
try {
187-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Image);
232+
ref?.get?.().notify?.({
233+
eventName: MLKitView.detectionEvent,
234+
object: ref?.get?.(),
235+
data: JSON.parse(param0),
236+
type: DetectionType.Image
237+
});
188238
} catch (e) { }
189239
},
190240
onError(param0: string, param1: java.lang.Exception) {
@@ -197,8 +247,17 @@ export class MLKitView extends MLKitViewBase {
197247
if (!this.#onObjectListener && (this.detectionType === DetectionType.Object || this.detectionType === DetectionType.All)) {
198248
this.#onObjectListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
199249
onSuccess(param0: string) {
250+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
251+
if (!hasListener) {
252+
return;
253+
}
200254
try {
201-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Object);
255+
ref?.get?.().notify?.({
256+
eventName: MLKitView.detectionEvent,
257+
object: ref?.get?.(),
258+
data: JSON.parse(param0),
259+
type: DetectionType.Object
260+
});
202261
} catch (e) { }
203262
},
204263
onError(param0: string, param1: java.lang.Exception) {
@@ -211,8 +270,17 @@ export class MLKitView extends MLKitViewBase {
211270
if (!this.#onPoseListener && (this.detectionType === DetectionType.Pose || this.detectionType === DetectionType.All)) {
212271
this.#onPoseListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
213272
onSuccess(param0: string) {
273+
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
274+
if (!hasListener) {
275+
return;
276+
}
214277
try {
215-
ref?.get?.().onDetection?.(JSON.parse(param0), DetectionType.Pose);
278+
ref?.get?.().notify?.({
279+
eventName: MLKitView.detectionEvent,
280+
object: ref?.get?.(),
281+
data: JSON.parse(param0),
282+
type: DetectionType.Pose
283+
});
216284
} catch (e) { }
217285
},
218286
onError(param0: string, param1: java.lang.Exception) {
@@ -233,7 +301,7 @@ export class MLKitView extends MLKitViewBase {
233301
let formats;
234302
if (Array.isArray(value)) {
235303
if (value.indexOf(BarcodeFormats.ALL)) {
236-
formats = Array.create('io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat', 1);
304+
formats = Array.create('io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner$BarcodeFormat', 1);
237305
formats[0] = io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ALL;
238306
} else {
239307
formats = value.map(format => {

packages/mlkit-core/index.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { MLKitViewBase, DetectionType } from "./common";
2+
import { EventData } from '@nativescript/core';
3+
export interface DetectionEvent extends EventData {
4+
data: { [key: string]: any };
5+
type: DetectionType
6+
}
27
export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty as imageLablerConfidenceThresholdProperty, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from './common';
38
export declare class MLKitView extends MLKitViewBase {
49
static isAvailable(): boolean;
5-
onDetection: (data: {
6-
[key: string]: any;
7-
}, type: DetectionType) => void;
10+
static detectionEvent: string;
811
stopPreview(): void;
912
toggleCamera(): void;
1013
startPreview(): void;
1114
requestCameraPermission(): Promise<void>;
1215
hasCameraPermission(): boolean;
16+
on(event: 'detection', callback: (args: DetectionEvent) => void, thisArg?: any);
1317
}

0 commit comments

Comments
 (0)