1
1
import { BarcodeFormats , barcodeFormatsProperty , CameraPosition , cameraPositionProperty , DetectionType , detectionTypeProperty , faceDetectionMinFaceSizeProperty , faceDetectionPerformanceModeProperty , faceDetectionTrackingEnabledProperty , imageLabelerConfidenceThresholdProperty , MLKitViewBase , objectDetectionClassifyProperty , objectDetectionMultipleProperty , pauseProperty , torchOnProperty } from "./common" ;
2
- import { Application , Device , Utils , AndroidActivityRequestPermissionsEventData } from '@nativescript/core' ;
2
+ import { Application , Device , Utils , AndroidActivityRequestPermissionsEventData , ImageSource } from '@nativescript/core' ;
3
3
import lazy from '@nativescript/core/utils/lazy' ;
4
+ import { StillImageDetectionOptions } from "." ;
4
5
5
6
const DetectorType_All = lazy ( ( ) => io . github . triniwiz . fancycamera . DetectorType . All ) ;
6
7
const DetectorType_Barcode = lazy ( ( ) => io . github . triniwiz . fancycamera . DetectorType . Barcode ) ;
@@ -11,6 +12,7 @@ const DetectorType_None = lazy(() => io.github.triniwiz.fancycamera.DetectorType
11
12
const DetectorType_Object = lazy ( ( ) => io . github . triniwiz . fancycamera . DetectorType . Object ) ;
12
13
const DetectorType_Pose = lazy ( ( ) => io . github . triniwiz . fancycamera . DetectorType . Pose ) ;
13
14
const DetectorType_Text = lazy ( ( ) => io . github . triniwiz . fancycamera . DetectorType . Text ) ;
15
+ const DetectorType_Selfie = lazy ( ( ) => ( io as any ) . github . triniwiz . fancycamera . DetectorType . Selfie ) ;
14
16
15
17
const BARCODE_SCANNER_SUPPORTED = lazy ( ( ) => typeof io . github . triniwiz . fancycamera ?. barcodescanning ?. BarcodeScanner ) ;
16
18
const TEXT_RECOGNITION_SUPPORTED = lazy ( ( ) => typeof io . github . triniwiz . fancycamera ?. textrecognition ?. TextRecognition ) ;
@@ -136,6 +138,9 @@ export class MLKitView extends MLKitViewBase {
136
138
case DetectionType . Text :
137
139
type = DetectorType_Text ( ) ;
138
140
break ;
141
+ case DetectionType . Selfie :
142
+ type = DetectorType_Selfie ( ) ;
143
+ break ;
139
144
default :
140
145
type = DetectorType_None ( ) ;
141
146
break ;
@@ -490,4 +495,93 @@ export class MLKitView extends MLKitViewBase {
490
495
hasCameraPermission ( ) : boolean {
491
496
return this . #camera. hasCameraPermission ( ) ;
492
497
}
493
- }
498
+ }
499
+
500
+ export function detectWithStillImage ( image : any , options ?: StillImageDetectionOptions ) {
501
+ return new Promise ( ( resolve , reject ) => {
502
+ let nativeImage ;
503
+ let rotation = 0 ;
504
+ if ( image instanceof ImageSource ) {
505
+ nativeImage = image . android ;
506
+ rotation = image . rotationAngle ;
507
+ } else if ( image instanceof android . graphics . Bitmap ) {
508
+ nativeImage = image ;
509
+ } else {
510
+ reject ( 'Please use a valid Image' ) ;
511
+ }
512
+
513
+
514
+
515
+ let type = 9 /* None */
516
+ switch ( options ?. detectorType ) {
517
+ case DetectionType . All :
518
+ type = 7 ;
519
+ break ;
520
+ case DetectionType . Barcode :
521
+ type = 0 ;
522
+ break ;
523
+ case DetectionType . DigitalInk :
524
+ type = 1
525
+ break ;
526
+ case DetectionType . Face :
527
+ type = 2
528
+ break ;
529
+ case DetectionType . Image :
530
+ type = 3
531
+ break ;
532
+ case DetectionType . Object :
533
+ type = 4
534
+ break ;
535
+ case DetectionType . Pose :
536
+ type = 5
537
+ break ;
538
+ case DetectionType . Text :
539
+ type = 6
540
+ break ;
541
+ case DetectionType . Selfie :
542
+ type = 8 ;
543
+ break ;
544
+ default :
545
+ type = 9 ;
546
+ break ;
547
+ }
548
+
549
+
550
+ ( io as any ) . github . triniwiz . fancycamera . ML . processImage ( nativeImage , rotation || 0 , JSON . stringify ( {
551
+ ...{
552
+ barcodeScanning : {
553
+ barcodeFormat : [ 0 ]
554
+ } ,
555
+ faceDetection : { } ,
556
+ imageLabeling : { } ,
557
+ objectDetection : { } ,
558
+ selfieSegmentation : { }
559
+ } , ...options , ...{ detectorType : type }
560
+ } ) , new io . github . triniwiz . fancycamera . ImageAnalysisCallback ( {
561
+ onSuccess ( param0 : any ) {
562
+ const results = { }
563
+ const size = param0 . size ( ) ;
564
+ for ( let i = 0 ; i < size ; i ++ ) {
565
+ const item = param0 . get ( i ) ;
566
+ const type = item [ 0 ] ;
567
+ const result = item [ 1 ] ;
568
+ try {
569
+ if ( type . toString ( ) === DetectionType . Selfie ) {
570
+ results [ type ] = {
571
+ width : result . geWidth ( ) ,
572
+ height : result . getHeight ( ) ,
573
+ buffer : ( ArrayBuffer as any ) . from ( result . getBuffer ( ) )
574
+ }
575
+ } else {
576
+ results [ type ] = JSON . parse ( result . toString ( ) ) ;
577
+ }
578
+ } catch ( e ) { }
579
+ }
580
+ resolve ( results ) ;
581
+ } ,
582
+ onError ( param0 : string , param1 : java . lang . Exception ) {
583
+ reject ( param0 )
584
+ }
585
+ } ) )
586
+ } )
587
+ }
0 commit comments