iPad横屏模式下AVCaptureSession存在问题

15

我一直在尝试从前置摄像头捕获帧并在视图上呈现它。这是我的代码。

_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPreset640x480;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];

if (deviceInput) {
    [_session addInput:deviceInput];
}
else {
    DDLogInfo(@"Some wierd shit happened");
    return;
}

// Session output
/*
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:output];
output.metadataObjectTypes = @[AVMetadataObjectTypeFace];
AVCaptureConnection *connection = [output connectionWithMediaType:AVMediaTypeMetadata];
connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
*/

// Session output
AVCaptureMovieFileOutput *videoOutput = [[AVCaptureMovieFileOutput alloc] init];
[_session addOutput:videoOutput];
AVCaptureConnection *connection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
connection.videoOrientation = AVCaptureVideoOrientationPortrait;

// Preview layer
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.frame = CGRectMake(0, 0, self.middleView.frame.size.width, self.middleView.frame.size.height);
[self.middleView.layer addSublayer:_previewLayer];

[_session startRunning];
问题在于当我的iPad处于横向模式时,演示层上的图像会旋转90度。我该如何解决这个问题?我一直在浏览Stack Overflow,尝试使用AVCaptureConnection做些什么,但是都没有成功。
2个回答

25

问题已解决,现在在所有方向上都能正常运行。我们需要进行设置。

_previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

方法在哪里:

 - (AVCaptureVideoOrientation) videoOrientationFromCurrentDeviceOrientation {
switch (self.interfaceOrientation) {
    case UIInterfaceOrientationPortrait: {
        return AVCaptureVideoOrientationPortrait;
    }
    case UIInterfaceOrientationLandscapeLeft: {
        return AVCaptureVideoOrientationLandscapeLeft;
    }
    case UIInterfaceOrientationLandscapeRight: {
        return AVCaptureVideoOrientationLandscapeRight;
    }
    case UIInterfaceOrientationPortraitUpsideDown: {
        return AVCaptureVideoOrientationPortraitUpsideDown;
    }
}
}

在捕获输出时,我们还需要设置:

AVCaptureConnection *output2VideoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
output2VideoConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

1
不要忘记,当用户旋转设备时,您需要再次设置.connection.videoOrientation :) - He Yifei 何一非

14

Swift 3 中:

首先,我们需要像 MegaManX 一样设置 previewLayer.connection.videoOrientation

previewLayer.connection.videoOrientation = self.videoOrientationFromCurrentDeviceOrientation()

方法如下:

func videoOrientationFromCurrentDeviceOrientation() -> AVCaptureVideoOrientation {
    switch UIApplication.shared.statusBarOrientation {
    case .portrait:
        return AVCaptureVideoOrientation.portrait
    case .landscapeLeft:
        return AVCaptureVideoOrientation.landscapeLeft
    case .landscapeRight:
        return AVCaptureVideoOrientation.landscapeRight
    case .portraitUpsideDown:
        return AVCaptureVideoOrientation.portraitUpsideDown
    default:
        // Can this happen?
        return AVCaptureVideoOrientation.portrait
    }
}

当用户旋转设备时,我们还需要处理videoOrientation(方法来自这里

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
        self.previewLayer.connection.videoOrientation = self.videoOrientationFromCurrentDeviceOrientation()

        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            // Finish Rotation
    })

    super.viewWillTransition(to: size, with: coordinator)
}

当设备方向被锁定时该怎么办?viewWillTransitionToSize方法将不会被调用。如何处理这种情况。我必须在设备锁定时检测图片方向。 - mahbaleshwar hegde
当设备锁定时,我得到的状态栏方向是竖屏。因此,即使我在横屏模式下拍照,视频连接也会变成竖屏。有什么办法吗?你有什么想法吗? - mahbaleshwar hegde
@mahbaleshwarhegde 请尝试这个问题的答案 https://dev59.com/FG455IYBdhLWcg3wCfmC :) - He Yifei 何一非
@mahbaleshwarhegde,当用户拍照时,尝试强制要求其输入特定方向如何? :) https://dev59.com/318d5IYBdhLWcg3w3FRk - He Yifei 何一非
@mahbaleshwarhegde 或者之后可能更改照片的方向以正确显示?(就像iOS的“相机”应用程序一样) - He Yifei 何一非
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接