iOS:摄像头方向 [英] iOS: camera orientation

查看:1710
本文介绍了iOS:摄像头方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AVCaptureSession用相机捕获图像。



它工作正常,我开始相机,我可以得到输出。但是,在旋转设备时,我对视频方向有一些问题。



首先,我要支持横向左右方向,稍后可能是纵向模式。 / p>

我实现:

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
return UIInterfaceOrientationIsLandscapse(interfaceOrientation);
}

当我旋转设备时,会将应用程序从左侧横向或反之亦然,但我只有看到相机正确当我在景观左。当应用程式在横向右侧时,影片旋转180度。



非常感谢。



更新:



我试过Spectravideo328回答,但是当我尝试旋转设备和应用程序崩溃时出现错误。这是错误:

  [AVCaptureVideoPreviewLayer连接]:无法识别的选择器发送到实例0xf678210 

** *由于未捕获异常而终止应用程序NSInvalidArgumentException,原因:' - [AVCaptureVideoPreviewLayer连接]:无法识别的选择器发送到实例0xf678210'


b $ b

错误发生在这一行:

  AVCaptureConnection * previewLayerConnection = self.previewLayer.connection; 

我把它放在shouldAutorotateToInterfaceOrientation方法里面。
你知道这个错误的原因是什么?



谢谢

解决方案

默认的相机方向是奇怪的UIInterfaceOrientationLeft。



相机方向不随设备旋转而改变。他们是分开的。您必须手动调整相机方向:



将以下内容放入传递给InterfaceOrientation的方法中(也许您可以从上面的shouldAutorotateToInterfaceOrientation调用它,以使设备旋转,相机旋转):



您必须先获取预览图层连接

  AVCaptureConnection * previewLayerConnection = self.previewLayer.connection; 

if([previewLayerConnection isVideoOrientationSupported])
{
switch(toInterfaceOrientation)
{
case UIInterfaceOrientationPortrait:
[previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait] ;
break;
case UIInterfaceOrientationLandscapeRight:
[previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; // home按钮在右边。参考.h not doc
break;
case UIInterfaceOrientationLandscapeLeft:
[previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; //左边的主页按钮。参考.h not doc
break;
默认值:
[previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; // for portrait upside down。参考.h not doc
break;
}
}


I want to capture an image with the camera using AVCaptureSession.

It works ok, I start the camera and I can get the output. However, I have some problems with video orientation when I rotate the device.

First, I want to support landscape left and right orientations and may be portrait modes too later.

I implement:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ 
return UIInterfaceOrientationIsLandscapse(interfaceOrientation);
}

When I rotate the device, it rotates the app from landscape left to landscape right or vice versa, but I only see the camera correctly when I'm on landscape left. When the app is on landscape right, the video is rotated by 180 degrees.

Thank you very much.

Update:

I've tried Spectravideo328 answer but I have an error when I try to rotate the device and the app crash. This is the error:

[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVCaptureVideoPreviewLayer connection]: unrecognized selector sent to instance 0xf678210'

The error occurs in this line:

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

I put it inside shouldAutorotateToInterfaceOrientation method. Do you know what could be the reason of this error?

Thanks

解决方案

The default camera orientation is oddly enough UIInterfaceOrientationLeft.

The camera orientation does not change with the rotation of the device. They are separate. You have to adjust the camera orientation manually:

Put the following in a method that you pass toInterfaceOrientation to (maybe you call it from shouldAutorotateToInterfaceOrientation above so that the device rotates and the camera rotates):

You have to get the preview Layer connection first

AVCaptureConnection *previewLayerConnection=self.previewLayer.connection;

if ([previewLayerConnection isVideoOrientationSupported])
{
    switch (toInterfaceOrientation)
    {
        case UIInterfaceOrientationPortrait:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft]; //home button on left. Refer to .h not doc
            break;
        default:
            [previewLayerConnection setVideoOrientation:AVCaptureVideoOrientationPortrait]; //for portrait upside down. Refer to .h not doc
            break;
    }
}

这篇关于iOS:摄像头方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆