From 10fea40a45aee77d2773d27bc00a62b6ddcdac3a Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Thu, 29 Sep 2022 12:10:30 -0400 Subject: [PATCH] feat(core): allow aspectRatio configuration --- packages/mlkit-barcode-scanning/package.json | 2 +- packages/mlkit-core/common.ts | 8 ++++++ packages/mlkit-core/index.ios.ts | 28 +++++++++++++++++-- packages/mlkit-core/package.json | 2 +- packages/mlkit-face-detection/package.json | 2 +- packages/mlkit-image-labeling/package.json | 2 +- packages/mlkit-object-detection/package.json | 2 +- packages/mlkit-pose-detection/package.json | 2 +- .../mlkit-selfie-segmentation/package.json | 2 +- packages/mlkit-text-recognition/package.json | 2 +- 10 files changed, 42 insertions(+), 10 deletions(-) diff --git a/packages/mlkit-barcode-scanning/package.json b/packages/mlkit-barcode-scanning/package.json index d692bf2..8f64686 100644 --- a/packages/mlkit-barcode-scanning/package.json +++ b/packages/mlkit-barcode-scanning/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-barcode-scanning", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Barcode Scanner module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-core/common.ts b/packages/mlkit-core/common.ts index 857442c..f8901a3 100644 --- a/packages/mlkit-core/common.ts +++ b/packages/mlkit-core/common.ts @@ -58,6 +58,7 @@ export class MLKitViewBase extends ContainerView { processEveryNthFrame: number; readonly latestImage?: ImageSource; retrieveLatestImage: boolean; + aspectRatio: 'aspect' | 'aspectFill' | 'fill'; } export const cameraPositionProperty = new Property({ @@ -67,6 +68,13 @@ export const cameraPositionProperty = new Property({ + name: 'aspectRatio', + defaultValue: 'aspect', +}); + +aspectRatioProperty.register(MLKitViewBase); + export const detectionTypeProperty = new Property({ name: 'detectionType', defaultValue: DetectionType.None, diff --git a/packages/mlkit-core/index.ios.ts b/packages/mlkit-core/index.ios.ts index ec40155..b93cfe0 100644 --- a/packages/mlkit-core/index.ios.ts +++ b/packages/mlkit-core/index.ios.ts @@ -1,5 +1,5 @@ import { ImageSource, Utils } from '@nativescript/core'; -import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, processEveryNthFrameProperty, torchOnProperty } from './common'; +import { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionProperty, DetectionType, detectionTypeProperty, faceDetectionMinFaceSizeProperty, faceDetectionPerformanceModeProperty, faceDetectionTrackingEnabledProperty, imageLabelerConfidenceThresholdProperty, MLKitViewBase, objectDetectionClassifyProperty, objectDetectionMultipleProperty, pauseProperty, processEveryNthFrameProperty, torchOnProperty, aspectRatioProperty } from './common'; import '@nativescript/core'; import lazy from '@nativescript/core/utils/lazy'; import { DetectionEvent, StillImageDetectionOptions } from '.'; @@ -17,6 +17,18 @@ export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionP declare const TNSMLKitHelper, TNSMLKitHelperCameraPosition; +function getGravity(value) { + switch (value) { + case 'fill': + return AVLayerVideoGravityResize; + case 'aspectFill': + return AVLayerVideoGravityResizeAspectFill; + case 'aspect': + return AVLayerVideoGravityResizeAspect; + } + return null; +} + export class MLKitView extends MLKitViewBase { #device: AVCaptureDevice; #preview: AVCaptureVideoPreviewLayer; @@ -68,7 +80,7 @@ export class MLKitView extends MLKitViewBase { createNativeView() { const nativeView = UIView.new(); this.#preview = AVCaptureVideoPreviewLayer.layerWithSession(this.#mlkitHelper.session); - this.#preview.videoGravity = AVLayerVideoGravityResizeAspect; + this.#preview.videoGravity = getGravity(this.aspectRatio) ?? AVLayerVideoGravityResizeAspect; nativeView.layer.insertSublayerAtIndex(this.#preview, 0); return nativeView; } @@ -145,6 +157,18 @@ export class MLKitView extends MLKitViewBase { return this.#selfieSegmentor; } + [aspectRatioProperty.setNative](ratio) { + if (this.#preview) { + switch (ratio) { + case 'fill': + case 'aspectFill': + case 'aspect': + this.#preview.videoGravity = getGravity(ratio); + break; + } + } + } + [cameraPositionProperty.setNative](value: CameraPosition) { switch (value) { case CameraPosition.FRONT: diff --git a/packages/mlkit-core/package.json b/packages/mlkit-core/package.json index 79f57a0..c155683 100644 --- a/packages/mlkit-core/package.json +++ b/packages/mlkit-core/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-core", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Core", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-face-detection/package.json b/packages/mlkit-face-detection/package.json index 63620b7..2f8b5ff 100644 --- a/packages/mlkit-face-detection/package.json +++ b/packages/mlkit-face-detection/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-face-detection", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Face Detection module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-image-labeling/package.json b/packages/mlkit-image-labeling/package.json index 53376ae..ffad14e 100644 --- a/packages/mlkit-image-labeling/package.json +++ b/packages/mlkit-image-labeling/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-image-labeling", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Image Labeling module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-object-detection/package.json b/packages/mlkit-object-detection/package.json index fca8770..8d3da92 100644 --- a/packages/mlkit-object-detection/package.json +++ b/packages/mlkit-object-detection/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-object-detection", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Object Detection module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-pose-detection/package.json b/packages/mlkit-pose-detection/package.json index 4db6cfd..7e5238d 100644 --- a/packages/mlkit-pose-detection/package.json +++ b/packages/mlkit-pose-detection/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-pose-detection", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Pose Detection module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-selfie-segmentation/package.json b/packages/mlkit-selfie-segmentation/package.json index b1bfef3..85ea7cc 100644 --- a/packages/mlkit-selfie-segmentation/package.json +++ b/packages/mlkit-selfie-segmentation/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-selfie-segmentation", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Self Segmentation module", "main": "index", "typings": "index.d.ts", diff --git a/packages/mlkit-text-recognition/package.json b/packages/mlkit-text-recognition/package.json index 7d9a24b..e281c6a 100644 --- a/packages/mlkit-text-recognition/package.json +++ b/packages/mlkit-text-recognition/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/mlkit-text-recognition", - "version": "1.0.5", + "version": "1.0.6", "description": "NativeScript MLKit Text Recognition module", "main": "index", "typings": "index.d.ts",