Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Allow mlkit textrecognition to work in landscape mode on iOS #1572

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions src/mlkit/mlkit-cameraview.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,22 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
this.previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(this.captureSession);
this.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

if (iosUtils.isLandscape()) {
const deviceOrientation = UIDevice.currentDevice.orientation;
this.previewLayer.connection.videoOrientation = deviceOrientation === UIDeviceOrientation.LandscapeLeft ? AVCaptureVideoOrientation.LandscapeRight : AVCaptureVideoOrientation.LandscapeLeft;
} else {
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
switch (UIDevice.currentDevice.orientation) {
case UIDeviceOrientation.LandscapeLeft:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight;
break;
case UIDeviceOrientation.LandscapeRight:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft;
break;
case UIDeviceOrientation.Portrait:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
break;
case UIDeviceOrientation.PortraitUpsideDown:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown;
break;
default:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
break;
}

// note that when rotating back to portrait, this event fires very late.. not much we can do I think
Expand All @@ -95,8 +106,22 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
this.cameraView.processEveryXFrames = this.processEveryNthFrame;

// this orientation is how the captured image is rotated (and shown)
if (this.rotateRecording()) {
this.cameraView.imageOrientation = UIImageOrientation.Right;
switch (UIDevice.currentDevice.orientation) {
case UIDeviceOrientation.Portrait:
this.cameraView.imageOrientation = UIImageOrientation.Right;
break;
case UIDeviceOrientation.PortraitUpsideDown:
this.cameraView.imageOrientation = UIImageOrientation.Left;
break;
case UIDeviceOrientation.LandscapeLeft:
this.cameraView.imageOrientation = UIImageOrientation.Up;
break;
case UIDeviceOrientation.LandscapeRight:
this.cameraView.imageOrientation = UIImageOrientation.Down;
break;
default:
this.cameraView.imageOrientation = UIImageOrientation.Right;
break;
}

this.cameraView.delegate = TNSMLKitCameraViewDelegateImpl.createWithOwnerResultCallbackAndOptions(
Expand All @@ -109,13 +134,43 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {

private rotateOnOrientationChange(args: OrientationChangedEventData): void {
if (this.previewLayer) {
if (args.newValue === "landscape") {
const deviceOrientation = UIDevice.currentDevice.orientation;
this.previewLayer.connection.videoOrientation = deviceOrientation === UIDeviceOrientation.LandscapeLeft ? AVCaptureVideoOrientation.LandscapeRight : AVCaptureVideoOrientation.LandscapeLeft;
} else if (args.newValue === "portrait") {
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
switch (UIDevice.currentDevice.orientation) {
case UIDeviceOrientation.LandscapeLeft:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight;
break;
case UIDeviceOrientation.LandscapeRight:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft;
break;
case UIDeviceOrientation.Portrait:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
break;
case UIDeviceOrientation.PortraitUpsideDown:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown;
break;
default:
this.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
break;
}
}

// this orientation is how the captured image is rotated (and shown)
switch (UIDevice.currentDevice.orientation) {
case UIDeviceOrientation.Portrait:
this.cameraView.imageOrientation = UIImageOrientation.Right;
break;
case UIDeviceOrientation.PortraitUpsideDown:
this.cameraView.imageOrientation = UIImageOrientation.Left;
break;
case UIDeviceOrientation.LandscapeLeft:
this.cameraView.imageOrientation = UIImageOrientation.Up;
break;
case UIDeviceOrientation.LandscapeRight:
this.cameraView.imageOrientation = UIImageOrientation.Down;
break;
default:
this.cameraView.imageOrientation = UIImageOrientation.Right;
break;
}
}

public onLayout(left: number, top: number, right: number, bottom: number): void {
Expand Down
4 changes: 4 additions & 0 deletions src/mlkit/textrecognition/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ios as iosUtils } from "tns-core-modules/utils/utils";
import { ImageSource } from "tns-core-modules/image-source";
import { MLKitVisionOptions } from "../";
import { MLKitRecognizeTextCloudOptions, MLKitRecognizeTextOnDeviceOptions, MLKitRecognizeTextResult } from "./";
Expand Down Expand Up @@ -25,6 +26,9 @@ export class MLKitTextRecognition extends MLKitTextRecognitionBase {
}

protected rotateRecording(): boolean {
if (iosUtils.isLandscape()) {
return false;
}
return true;
}
}
Expand Down