Ionic Angular Camera Preview Plugin: How to Keep AVCaptureDeviceTypeBuiltInTripleCamera in...

Discussão em 'Angular' iniciado por Hasan Ali, Janeiro 6, 2025.

  1. Hasan Ali

    Hasan Ali Guest

    I am developing an app using Ionic Angular, and I am using a camera preview plugin to display the camera feed. I made modifications in the native code (Objective-C) to use the AVCaptureDeviceTypeBuiltInTripleCamera for newer iPhones. The camera works as expected, but the preview is always rotated like landscape mode.

    I want to ensure the camera always displays in portrait orientation, regardless of the device's physical orientation.

    Here’s what I’ve done so far:

    - (void)startCamera:(CDVInvokedUrlCommand*)command {
    CDVPluginResult *pluginResult;

    if (self.sessionManager != nil) {
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:mad:"Camera already started!"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    return;
    }

    if (command.arguments.count > 3) {
    CGFloat x = (CGFloat)[command.arguments[0] floatValue] + self.webView.frame.origin.x;
    CGFloat y = (CGFloat)[command.arguments[1] floatValue] + self.webView.frame.origin.y;
    CGFloat width = (CGFloat)[command.arguments[2] floatValue];
    CGFloat height = (CGFloat)[command.arguments[3] floatValue];
    NSString *defaultCamera = command.arguments[4];
    BOOL tapToTakePicture = (BOOL)[command.arguments[5] boolValue];
    BOOL dragEnabled = (BOOL)[command.arguments[6] boolValue];
    BOOL toBack = (BOOL)[command.arguments[7] boolValue];
    CGFloat alpha = (CGFloat)[command.arguments[8] floatValue];
    BOOL tapToFocus = (BOOL) [command.arguments[9] boolValue];
    BOOL disableExifHeaderStripping = (BOOL) [command.arguments[10] boolValue]; // ignore Android only
    self.storeToFile = (BOOL) [command.arguments[11] boolValue];

    // Create the session manager
    self.sessionManager = [[CameraSessionManager alloc] init];

    // render controller setup
    self.cameraRenderController = [[CameraRenderController alloc] init];
    self.cameraRenderController.dragEnabled = dragEnabled;
    self.cameraRenderController.tapToTakePicture = tapToTakePicture;
    self.cameraRenderController.tapToFocus = tapToFocus;
    self.cameraRenderController.sessionManager = self.sessionManager;
    self.cameraRenderController.view.frame = CGRectMake(x, y, width, height);
    self.cameraRenderController.delegate = self;

    [self.viewController addChildViewController:self.cameraRenderController];

    if (toBack) {
    // Display the camera below the webview

    // Make transparent
    self.webView.opaque = NO;
    self.webView.backgroundColor = [UIColor clearColor];

    [self.webView.superview addSubview:self.cameraRenderController.view];
    [self.webView.superview bringSubviewToFront:self.webView];
    } else {
    self.cameraRenderController.view.alpha = alpha;
    [self.webView.superview insertSubview:self.cameraRenderController.view aboveSubview:self.webView];
    }

    // Setup session with ultra-wide or default camera
    self.sessionManager.delegate = self.cameraRenderController;

    [self.sessionManager setupSession:defaultCamera completion:^(BOOL started) {
    if (started) {
    // Configure ultra-wide camera if session started successfully
    [self.sessionManager configureUltraWideCamera];

    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
    } else {
    [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:mad:"Failed to start camera"] callbackId:command.callbackId];
    }
    }];

    } else {
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:mad:"Invalid number of parameters"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    }



    its rotating only when i call [self.sessionManager configureUltraWideCamera];

    - (void)configureUltraWideCamera {
    NSError *error = nil;

    // Find an AVCaptureDevice of type ultra-wide camera
    AVCaptureDevice *ultraWideCamera = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInTripleCamera
    mediaType:AVMediaTypeVideo
    position:AVCaptureDevicePositionBack];

    if (ultraWideCamera) {
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:ultraWideCamera error:&error];

    if (!error && input) {
    [self.session beginConfiguration]; // Begin session reconfiguration
    [self.session removeInput:self.videoDeviceInput]; // Remove current input
    if ([self.session canAddInput:input]) {
    [self.session addInput:input]; // Add ultra-wide input
    self.videoDeviceInput = input; // Update the input property

    // Commit the session configuration
    [self.session commitConfiguration];
    [self.session startRunning];



    NSLog(@"Switched to Ultra-Wide Camera and locked orientation to portrait");
    } else {
    [self.session commitConfiguration];
    NSLog(@"Cannot add ultra-wide camera input to the session");
    }
    } else {
    NSLog(@"Error creating input for ultra-wide camera: %@", error.localizedDescription);
    }
    } else {
    NSLog(@"Ultra-Wide Camera is not supported on this device");
    }
    }


    Modified the plugin's Objective-C code to use AVCaptureDeviceTypeBuiltInTripleCamera to enable the ultra-wide camera for newer iPhones.

    Continue reading...

Compartilhe esta Página