Skip to content

Commit

Permalink
feat(core): allow aspectRatio configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Sep 29, 2022
1 parent db0b4a0 commit 10fea40
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/mlkit-barcode-scanning/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 8 additions & 0 deletions packages/mlkit-core/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MLKitViewBase, CameraPosition>({
Expand All @@ -67,6 +68,13 @@ export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition

cameraPositionProperty.register(MLKitViewBase);

export const aspectRatioProperty = new Property<MLKitViewBase, 'aspect' | 'aspectFill' | 'fill'>({
name: 'aspectRatio',
defaultValue: 'aspect',
});

aspectRatioProperty.register(MLKitViewBase);

export const detectionTypeProperty = new Property<MLKitViewBase, DetectionType>({
name: 'detectionType',
defaultValue: DetectionType.None,
Expand Down
28 changes: 26 additions & 2 deletions packages/mlkit-core/index.ios.ts
Original file line number Diff line number Diff line change
@@ -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 '.';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-face-detection/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-image-labeling/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-object-detection/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-pose-detection/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-selfie-segmentation/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mlkit-text-recognition/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 10fea40

Please sign in to comment.