gpt4 book ai didi

ios - 具有多个方向问题的 AVCaptureSession

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:54 26 4
gpt4 key购买 nike

我正在尝试实现条码扫描器。我有一个从 AVCaptureDevice 接收视频的 AVCaptureSession。我想支持所有方向。使用以下代码,当我运行该应用程序时,纵向方向一切正常。然而,在横向模式下, View 会旋转,但视频输入不会。所以我最终得到了一个 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;
}

*/

编辑:

我尝试了以下代码,但 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];
}

最佳答案

Ah 用以下方法修复了它

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

关于ios - 具有多个方向问题的 AVCaptureSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com