具有多个方向问题的AVCaptureSession [英] AVCaptureSession with multiple orientations issue
问题描述
我正在尝试实施条形码扫描仪。我有一个AVCaptureSession从AVCaptureDevice接收视频。我想支持所有方向。使用以下代码,当我运行应用程序时,纵向方向的一切都很好。但是在横向方向上,视图会旋转但视频输入不会旋转。所以我最终得到了90度旋转视频。
I am attempting to implement a barcode scanner. I have an AVCaptureSession that takes in video from AVCaptureDevice. I want to support all orientations. With the following code, when I run the app, everything is fine in the portrait orientation. However in landscape orientation, the view rotates but the video input does not. So I end up with a 90degree rotated video.
当我实现 - (NSUInteger)supportedInterfaceOrientations方法时,一切都被锁定到纵向位置。
When I implement -(NSUInteger)supportedInterfaceOrientations method, then everything gets locked to the portrait position.
谁能告诉我如何解决这个问题?
Can anyone tell me how I can fix this issue?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupCaptureSession];
_previewLayer.frame = _previewView.bounds;
_previewView.center = self.view.center;
_previewView.backgroundColor = [UIColor redColor];
[_previewView.layer addSublayer:_previewLayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startRunning];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self stopRunning];
}
-(void) setupCaptureSession
{
if (_captureSession)
return;
_videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!_videoDevice)
{
NSLog(@"No video camera on this device");
return;
}
_captureSession = [[AVCaptureSession alloc]init];
_videoInput = [[AVCaptureDeviceInput alloc]initWithDevice:_videoDevice error:nil];
if ([_captureSession canAddInput:_videoInput])
{
[_captureSession addInput:_videoInput];
}
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
}
-(void) startRunning
{
if (_running)
return;
[_captureSession startRunning];
_running = YES;
}
- (void) stopRunning
{
if (!_running)
return;
[_captureSession stopRunning];
_running = NO;
}
/*
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationPortrait|UIInterfaceOrientationPortraitUpsideDown;
}
*/
编辑:
我尝试了以下代码,但previewLayer方向仍然是颠倒/横向。
I tried the following code but the previewLayer orientation is still upsidedown/sideways.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
-(void) orientationChanged
{
if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}
推荐答案
用以下内容修复它
-(void) orientationChanged {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
else if (deviceOrientation == UIInterfaceOrientationPortrait)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
else
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}
这篇关于具有多个方向问题的AVCaptureSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!