generated from NativeScript/plugin-seed
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.android.ts
607 lines (547 loc) · 22 KB
/
index.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, processEveryNthFrameProperty, torchOnProperty } from './common';
import { Application, Device, Utils, AndroidActivityRequestPermissionsEventData, ImageSource } from '@nativescript/core';
import lazy from '@nativescript/core/utils/lazy';
import { StillImageDetectionOptions } from '.';
const DetectorType_All = lazy(() => io.github.triniwiz.fancycamera.DetectorType.All);
const DetectorType_Barcode = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Barcode);
const DetectorType_DigitalInk = lazy(() => io.github.triniwiz.fancycamera.DetectorType.DigitalInk);
const DetectorType_Face = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Face);
const DetectorType_Image = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Image);
const DetectorType_None = lazy(() => io.github.triniwiz.fancycamera.DetectorType.None);
const DetectorType_Object = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Object);
const DetectorType_Pose = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Pose);
const DetectorType_Text = lazy(() => io.github.triniwiz.fancycamera.DetectorType.Text);
const DetectorType_Selfie = lazy(() => (io as any).github.triniwiz.fancycamera.DetectorType.Selfie);
const BARCODE_SCANNER_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.barcodescanning?.BarcodeScanner !== 'undefined');
const TEXT_RECOGNITION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.textrecognition?.TextRecognition !== 'undefined');
const FACE_DETECTION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.facedetection?.FaceDetection !== 'undefined');
const IMAGE_LABELING_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.imagelabeling?.ImageLabeling !== 'undefined');
const OBJECT_DETECTION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.objectdetection?.ObjectDetection !== 'undefined');
const POSE_DETECTION_SUPPORTED = lazy(() => typeof io.github.triniwiz.fancycamera?.posedetection?.PoseDetection !== 'undefined');
const TORCH_MODE_ON = lazy(() => io.github.triniwiz.fancycamera.CameraFlashMode.TORCH);
const TORCH_MODE_OFF = lazy(() => io.github.triniwiz.fancycamera.CameraFlashMode.OFF);
export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, objectDetectionClassifyProperty, objectDetectionMultipleProperty } from './common';
export class MLKitView extends MLKitViewBase {
_camera: io.github.triniwiz.fancycamera.FancyCamera;
static _hasCamera: boolean;
_onTextListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onBarcodeListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onDigitalInkListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onFaceListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onImageListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onObjectListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_onPoseListener: io.github.triniwiz.fancycamera.ImageAnalysisCallback;
_permissionHandler = (args: AndroidActivityRequestPermissionsEventData) => {
if (this.pause) {
return;
}
this._camera.onPermissionHandler(args.requestCode, args.permissions, args.grantResults);
};
_barcodeScannerOptions: io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.Options;
_faceDetectionOptions: io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options;
_imageLabelerOptions: io.github.triniwiz.fancycamera.imagelabeling.ImageLabeling.Options;
_objectDetectionOptions: io.github.triniwiz.fancycamera.objectdetection.ObjectDetection.Options;
_getFancyCamera() {
return (this._camera as any)?.getChildAt?.(0) as io.github.triniwiz.fancycamera.CameraBase;
}
createNativeView() {
this._camera = new io.github.triniwiz.fancycamera.FancyCamera(this._context);
Application.android.on('activityRequestPermissions', this._permissionHandler);
return this._camera;
}
//@ts-ignore
get retrieveLatestImage(): boolean {
if (!this._camera) {
return false;
}
return this._camera.getRetrieveLatestImage();
}
set retrieveLatestImage(value: boolean) {
this._camera.setRetrieveLatestImage(value);
}
_latestImage: ImageSource = null;
//@ts-ignore
get latestImage(): ImageSource {
if (!this._camera) {
return null;
}
const image = this._camera.getLatestImage();
if (!image) {
return null;
}
if (image !== this._latestImage?.android) {
this._latestImage = new ImageSource(image);
}
return this._latestImage;
}
[cameraPositionProperty.setNative](value: CameraPosition) {
if (this._camera) {
switch (value) {
case CameraPosition.FRONT:
this._camera.setPosition(io.github.triniwiz.fancycamera.CameraPosition.FRONT);
break;
default:
this._camera.setPosition(io.github.triniwiz.fancycamera.CameraPosition.BACK);
break;
}
}
}
public static isAvailable() {
if (this._hasCamera === undefined) {
if (parseInt(Device.sdkVersion) >= 21) {
try {
const manager: android.hardware.camera2.CameraManager = Utils.android.getApplicationContext().getSystemService(android.content.Context.CAMERA_SERVICE);
this._hasCamera = manager.getCameraIdList().length > 0;
} catch (e) {
this._hasCamera = false;
}
} else {
this._hasCamera = android.hardware.Camera.getNumberOfCameras() > 0;
}
}
return this._hasCamera;
}
[torchOnProperty.setNative](value: boolean) {
if (this._camera) {
if (value) {
this._camera.setFlashMode(TORCH_MODE_ON());
} else {
this._camera.setFlashMode(TORCH_MODE_OFF());
}
}
}
[pauseProperty.setNative](value: boolean) {
if (this._camera) {
this._camera.setPause(value);
}
}
[detectionTypeProperty.setNative](value) {
let type = DetectorType_None();
switch (value) {
case DetectionType.All:
type = DetectorType_All();
break;
case DetectionType.Barcode:
type = DetectorType_Barcode();
break;
case DetectionType.DigitalInk:
type = DetectorType_DigitalInk();
break;
case DetectionType.Face:
type = DetectorType_Face();
break;
case DetectionType.Image:
type = DetectorType_Image();
break;
case DetectionType.Object:
type = DetectorType_Object();
break;
case DetectionType.Pose:
type = DetectorType_Pose();
break;
case DetectionType.Text:
type = DetectorType_Text();
break;
case DetectionType.Selfie:
type = DetectorType_Selfie();
break;
default:
type = DetectorType_None();
break;
}
this._camera.setDetectorType(type);
this._setListeners();
}
[processEveryNthFrameProperty.setNative](value) {
if (this._camera) {
this._camera.setProcessEveryNthFrame(value);
}
}
initNativeView() {
super.initNativeView();
this._setListeners();
}
_setListeners() {
const ref = new WeakRef(this);
if (!this._onTextListener && (this.detectionType === DetectionType.Text || this.detectionType === DetectionType.All)) {
this._onTextListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Text,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnTextRecognitionListener(this._onTextListener);
}
if (!this._onBarcodeListener && (this.detectionType.includes(DetectionType.Barcode) || this.detectionType.includes(DetectionType.All))) {
this._onBarcodeListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Barcode,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnBarcodeScanningListener(this._onBarcodeListener);
}
// todo
if (!this._onDigitalInkListener && (this.detectionType === DetectionType.DigitalInk || this.detectionType === DetectionType.All)) {
/* this._onDigitalInkListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if(!hasListener){
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.DigitalInk
});
} catch (e) { }
},
onError(param0: string, param1: java.lang.Exception) {
}
}); */
}
if (!this._onFaceListener && (this.detectionType === DetectionType.Face || this.detectionType === DetectionType.All)) {
if (FACE_DETECTION_SUPPORTED()) {
this._faceDetectionOptions = new io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options();
}
this._onFaceListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Face,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnFacesDetectedListener(this._onFaceListener);
}
if (!this._onImageListener && (this.detectionType === DetectionType.Image || this.detectionType === DetectionType.All)) {
this._onImageListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Image,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnImageLabelingListener(this._onImageListener);
}
if (!this._onObjectListener && (this.detectionType === DetectionType.Object || this.detectionType === DetectionType.All)) {
this._onObjectListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Object,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnObjectDetectedListener(this._onObjectListener);
}
if (!this._onPoseListener && (this.detectionType === DetectionType.Pose || this.detectionType === DetectionType.All)) {
this._onPoseListener = new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: string) {
const hasListener = ref?.get?.().hasListeners?.(MLKitView.detectionEvent);
if (!hasListener) {
return;
}
try {
ref?.get?.().notify?.({
eventName: MLKitView.detectionEvent,
object: ref?.get?.(),
data: JSON.parse(param0),
type: DetectionType.Pose,
});
} catch (e) {}
},
onError(param0: string, param1: java.lang.Exception) {},
});
this._camera.setOnPoseDetectedListener(this._onPoseListener);
}
}
[barcodeFormatsProperty.setNative](value: BarcodeFormats[]) {
if (!BARCODE_SCANNER_SUPPORTED()) {
return;
}
if (!this._barcodeScannerOptions) {
this._barcodeScannerOptions = new io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.Options();
}
let formats;
if (Array.isArray(value)) {
if (value.indexOf(BarcodeFormats.ALL) !== -1) {
formats = Array.create('io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner$BarcodeFormat', 1);
formats[0] = io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ALL;
} else {
formats = value.map((format) => {
switch (format) {
case BarcodeFormats.AZTEC:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.AZTEC;
case BarcodeFormats.CODABAR:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.CODABAR;
case BarcodeFormats.CODE_128:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.CODE_128;
case BarcodeFormats.CODE_39:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.CODE_39;
case BarcodeFormats.CODE_93:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.CODE_93;
case BarcodeFormats.DATA_MATRIX:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.DATA_MATRIX;
case BarcodeFormats.EAN_13:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.EAN_13;
case BarcodeFormats.EAN_8:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.EAN_8;
case BarcodeFormats.ITF:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ITF;
case BarcodeFormats.PDF417:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.PDF417;
case BarcodeFormats.QR_CODE:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.QR_CODE;
case BarcodeFormats.UPC_A:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.UPC_A;
case BarcodeFormats.UPC_E:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.UPC_E;
default:
return io.github.triniwiz.fancycamera.barcodescanning.BarcodeScanner.BarcodeFormat.ALL;
}
});
}
this._barcodeScannerOptions.setBarcodeFormat(formats);
}
this._camera.setBarcodeScannerOptions(this._barcodeScannerOptions);
}
[faceDetectionTrackingEnabledProperty.setNative](value) {
if (!FACE_DETECTION_SUPPORTED()) {
return;
}
if (!this._faceDetectionOptions) {
this._faceDetectionOptions = new io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options();
}
this._faceDetectionOptions.setFaceTracking(value);
this._camera.setFaceDetectionOptions(this._faceDetectionOptions);
}
[faceDetectionMinFaceSizeProperty.setNative](value) {
if (!FACE_DETECTION_SUPPORTED()) {
return;
}
if (!this._faceDetectionOptions) {
this._faceDetectionOptions = new io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options();
}
this._faceDetectionOptions.setMinimumFaceSize(value);
this._camera.setFaceDetectionOptions(this._faceDetectionOptions);
}
[faceDetectionPerformanceModeProperty.setNative](value) {
if (!FACE_DETECTION_SUPPORTED()) {
return;
}
if (!this._faceDetectionOptions) {
this._faceDetectionOptions = new io.github.triniwiz.fancycamera.facedetection.FaceDetection.Options();
}
this._faceDetectionOptions.setMinimumFaceSize(value);
this._camera.setFaceDetectionOptions(this._faceDetectionOptions);
}
[imageLabelerConfidenceThresholdProperty.setNative](value) {
if (!IMAGE_LABELING_SUPPORTED()) {
return;
}
if (!this._imageLabelerOptions) {
this._imageLabelerOptions = new io.github.triniwiz.fancycamera.imagelabeling.ImageLabeling.Options();
}
this._imageLabelerOptions.setConfidenceThreshold(value);
this._camera.setImageLabelingOptions(this._imageLabelerOptions);
}
[objectDetectionClassifyProperty.setNative](value) {
if (!OBJECT_DETECTION_SUPPORTED()) {
return;
}
if (!this._objectDetectionOptions) {
this._objectDetectionOptions = new io.github.triniwiz.fancycamera.objectdetection.ObjectDetection.Options();
}
this._objectDetectionOptions.setClassification(value);
this._camera.setObjectDetectionOptions(this._objectDetectionOptions);
}
[objectDetectionMultipleProperty.setNative](value) {
if (!OBJECT_DETECTION_SUPPORTED()) {
return;
}
if (!this._objectDetectionOptions) {
this._objectDetectionOptions = new io.github.triniwiz.fancycamera.objectdetection.ObjectDetection.Options();
}
this._objectDetectionOptions.setMultiple(value);
this._camera.setObjectDetectionOptions(this._objectDetectionOptions);
}
onLoaded() {
super.onLoaded();
this.startPreview();
}
onUnloaded() {
this.stopPreview();
super.onUnloaded();
}
disposeNativeView() {
Application.android.off('activityRequestPermissions', this._permissionHandler);
super.disposeNativeView();
}
public stopPreview(): void {
this._camera.stopPreview();
}
public toggleCamera(): void {
this._camera.toggleCamera();
}
public startPreview(): void {
this._camera.startPreview();
}
requestCameraPermission() {
return new Promise<void>((resolve, reject) => {
Application.android.once('activityRequestPermissions', (args: AndroidActivityRequestPermissionsEventData) => {
if (this._camera.hasCameraPermission()) {
resolve();
} else {
reject();
}
});
this._camera.requestCameraPermission();
});
}
hasCameraPermission(): boolean {
return this._camera.hasCameraPermission();
}
}
export function detectWithStillImage(image: any, options?: StillImageDetectionOptions) {
return new Promise((resolve, reject) => {
let nativeImage;
let rotation = 0;
if (image instanceof ImageSource) {
nativeImage = image.android;
rotation = image.rotationAngle;
} else if (image instanceof android.graphics.Bitmap) {
nativeImage = image;
} else {
reject('Please use a valid Image');
}
let type = 9; /* None */
switch (options?.detectorType) {
case DetectionType.All:
type = 7;
break;
case DetectionType.Barcode:
type = 0;
break;
case DetectionType.DigitalInk:
type = 1;
break;
case DetectionType.Face:
type = 2;
break;
case DetectionType.Image:
type = 3;
break;
case DetectionType.Object:
type = 4;
break;
case DetectionType.Pose:
type = 5;
break;
case DetectionType.Text:
type = 6;
break;
case DetectionType.Selfie:
type = 8;
break;
default:
type = 9;
break;
}
(io as any).github.triniwiz.fancycamera.ML.processImage(
nativeImage,
rotation || 0,
JSON.stringify({
...{
barcodeScanning: {
barcodeFormat: [0],
},
faceDetection: {},
imageLabeling: {},
objectDetection: {},
selfieSegmentation: {},
},
...options,
...{ detectorType: type },
}),
new io.github.triniwiz.fancycamera.ImageAnalysisCallback({
onSuccess(param0: any) {
const results = {};
const size = param0.size();
for (let i = 0; i < size; i++) {
const item = param0.get(i);
const type = item[0];
const result = item[1];
try {
if (type.toString() === DetectionType.Selfie) {
results[type] = {
width: result.geWidth(),
height: result.getHeight(),
buffer: (ArrayBuffer as any).from(result.getBuffer()),
};
} else {
results[type] = JSON.parse(result.toString());
}
} catch (e) {}
}
resolve(results);
},
onError(param0: string, param1: java.lang.Exception) {
reject(param0);
},
})
);
});
}