具有多个方向问题的AVCaptureSession

Zia*_*Zia 2 iphone avfoundation ipad ios

我正在尝试实施条形码扫描仪.我有一个AVCaptureSession从AVCaptureDevice接收视频.我想支持所有方向.使用以下代码,当我运行应用程序时,纵向方向的一切都很好.但是在横向方向上,视图会旋转但视频输入不会旋转.所以我最终得到了90度旋转视频.

当我实现 - (NSUInteger)supportedInterfaceOrientations方法时,一切都被锁定到纵向位置.

谁能告诉我如何解决这个问题?

- (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;
}

*/
Run Code Online (Sandbox Code Playgroud)

编辑:

我尝试了以下代码,但previewLayer方向仍然是颠倒/侧面.

    [[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];
}
Run Code Online (Sandbox Code Playgroud)

Zia*_*Zia 11

啊修好了以下内容

-(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];
}
Run Code Online (Sandbox Code Playgroud)


Aya*_*pta 7

要在捕获的介质中反映设备方向更改,您需要设置videoOrientation输出捕获连接的属性.

AVCaptureOutput* output = <# Your capture output device #>; //reference to your device output, possibly of AVCaptureStillImageOutput or AVCaptureMovieFileOutput type
AVCaptureConnection* connection = [output connectionWithMediaType:AVMediaTypeVideo];
if ([connection isVideoOrientationSupported]) {
    connection.videoOrientation = [self videoOrientationFromDeviceOrientation];
}
Run Code Online (Sandbox Code Playgroud)

以下方法根据设备方向返回视频方向:

-(AVCaptureVideoOrientation)videoOrientationFromDeviceOrientation {
    AVCaptureVideoOrientation result = [UIDevice currentDevice].orientation;
    if ( result == UIDeviceOrientationLandscapeLeft )
        result = AVCaptureVideoOrientationLandscapeRight;
    else if ( result == UIDeviceOrientationLandscapeRight )
        result = AVCaptureVideoOrientationLandscapeLeft;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

希望你会发现它有用.