Skip to content

Commit 10fea40

Browse files
committed
feat(core): allow aspectRatio configuration
1 parent db0b4a0 commit 10fea40

File tree

10 files changed

+42
-10
lines changed

10 files changed

+42
-10
lines changed

packages/mlkit-barcode-scanning/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-barcode-scanning",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Barcode Scanner module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-core/common.ts

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class MLKitViewBase extends ContainerView {
5858
processEveryNthFrame: number;
5959
readonly latestImage?: ImageSource;
6060
retrieveLatestImage: boolean;
61+
aspectRatio: 'aspect' | 'aspectFill' | 'fill';
6162
}
6263

6364
export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition>({
@@ -67,6 +68,13 @@ export const cameraPositionProperty = new Property<MLKitViewBase, CameraPosition
6768

6869
cameraPositionProperty.register(MLKitViewBase);
6970

71+
export const aspectRatioProperty = new Property<MLKitViewBase, 'aspect' | 'aspectFill' | 'fill'>({
72+
name: 'aspectRatio',
73+
defaultValue: 'aspect',
74+
});
75+
76+
aspectRatioProperty.register(MLKitViewBase);
77+
7078
export const detectionTypeProperty = new Property<MLKitViewBase, DetectionType>({
7179
name: 'detectionType',
7280
defaultValue: DetectionType.None,

packages/mlkit-core/index.ios.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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';
33
import '@nativescript/core';
44
import lazy from '@nativescript/core/utils/lazy';
55
import { DetectionEvent, StillImageDetectionOptions } from '.';
@@ -17,6 +17,18 @@ export { BarcodeFormats, barcodeFormatsProperty, CameraPosition, cameraPositionP
1717

1818
declare 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+
2032
export 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:

packages/mlkit-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-core",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Core",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-face-detection/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-face-detection",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Face Detection module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-image-labeling/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-image-labeling",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Image Labeling module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-object-detection/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-object-detection",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Object Detection module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-pose-detection/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-pose-detection",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Pose Detection module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-selfie-segmentation/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-selfie-segmentation",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Self Segmentation module",
55
"main": "index",
66
"typings": "index.d.ts",

packages/mlkit-text-recognition/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/mlkit-text-recognition",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "NativeScript MLKit Text Recognition module",
55
"main": "index",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)