11import { ImageSource , Utils } from '@nativescript/core' ;
2- import { BarcodeFormats , barcodeFormatsProperty , CameraPosition , cameraPositionProperty , DetectionType , detectionTypeProperty , faceDetectionMinFaceSizeProperty , faceDetectionPerformanceModeProperty , faceDetectionTrackingEnabledProperty , imageLabelerConfidenceThresholdProperty , MLKitViewBase , objectDetectionClassifyProperty , objectDetectionMultipleProperty , pauseProperty , processEveryNthFrameProperty , torchOnProperty } from './common' ;
2+ import { BarcodeFormats , barcodeFormatsProperty , CameraPosition , cameraPositionProperty , DetectionType , detectionTypeProperty , faceDetectionMinFaceSizeProperty , faceDetectionPerformanceModeProperty , faceDetectionTrackingEnabledProperty , imageLabelerConfidenceThresholdProperty , MLKitViewBase , objectDetectionClassifyProperty , objectDetectionMultipleProperty , pauseProperty , processEveryNthFrameProperty , torchOnProperty , aspectRatioProperty } from './common' ;
33import '@nativescript/core' ;
44import lazy from '@nativescript/core/utils/lazy' ;
55import { DetectionEvent , StillImageDetectionOptions } from '.' ;
@@ -17,6 +17,18 @@ export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionP
1717
1818declare const TNSMLKitHelper , TNSMLKitHelperCameraPosition ;
1919
20+ function getGravity ( value ) {
21+ switch ( value ) {
22+ case 'fill' :
23+ return AVLayerVideoGravityResize ;
24+ case 'aspectFill' :
25+ return AVLayerVideoGravityResizeAspectFill ;
26+ case 'aspect' :
27+ return AVLayerVideoGravityResizeAspect ;
28+ }
29+ return null ;
30+ }
31+
2032export class MLKitView extends MLKitViewBase {
2133 #device: AVCaptureDevice ;
2234 #preview: AVCaptureVideoPreviewLayer ;
@@ -68,7 +80,7 @@ export class MLKitView extends MLKitViewBase {
6880 createNativeView ( ) {
6981 const nativeView = UIView . new ( ) ;
7082 this . #preview = AVCaptureVideoPreviewLayer . layerWithSession ( this . #mlkitHelper. session ) ;
71- this . #preview. videoGravity = AVLayerVideoGravityResizeAspect ;
83+ this . #preview. videoGravity = getGravity ( this . aspectRatio ) ?? AVLayerVideoGravityResizeAspect ;
7284 nativeView . layer . insertSublayerAtIndex ( this . #preview, 0 ) ;
7385 return nativeView ;
7486 }
@@ -145,6 +157,18 @@ export class MLKitView extends MLKitViewBase {
145157 return this . #selfieSegmentor;
146158 }
147159
160+ [ aspectRatioProperty . setNative ] ( ratio ) {
161+ if ( this . #preview) {
162+ switch ( ratio ) {
163+ case 'fill' :
164+ case 'aspectFill' :
165+ case 'aspect' :
166+ this . #preview. videoGravity = getGravity ( ratio ) ;
167+ break ;
168+ }
169+ }
170+ }
171+
148172 [ cameraPositionProperty . setNative ] ( value : CameraPosition ) {
149173 switch ( value ) {
150174 case CameraPosition . FRONT :
0 commit comments