ios – 同一AVCaptureSession中的多个AVCaptureVideoDataOutput

ios – 同一AVCaptureSession中的多个AVCaptureVideoDataOutput,第1张

概述我想知道是否可以使用单个摄像头设备输入将多个AVCaptureVideoDataOutput添加到AVCaptureSession? 我的实验索引添加第二个VideoDataOutput将导致canAddOutput返回NO.但我在Apple的文档中找不到任何地方说多个数据输出是不允许的. 我们不能将单个AVCaptureSession用于多个AVCaptureVideoDataOutput对象. 我想知道是否可以使用单个摄像头设备输入将多个AVCaptureVIDeoDataOutput添加到AVCaptureSession?

我的实验索引添加第二个VIDeoDataOutput将导致canAddOutput返回NO.但我在Apple的文档中找不到任何地方说多个数据输出是不允许的.

解决方法 我们不能将单个AVCaptureSession用于多个AVCaptureVIDeoDataOutput对象.

你能做的是你可以用多个AVCaptureSession对象制作多个AVCaptureVIDeoDataOutput.

您可以创建两个不同的AVCaptureVIDeoDataOutput和AVCaptureSession设置,然后您可以在应用程序中一个接一个地使用它们,您将能够实现目标.

在我的情况下,我不得不一次使用相机捕捉正面和背面图像.

我确实为AVCaptureVIDeoDataOutput和AVCaptureSession创建了两个不同的对象,如下所示.

/* Front camera settings */@property bool isFrontRecording;@property (strong,nonatomic) AVCaptureDeviceinput *vIDeoinputBack;@property (strong,nonatomic) AVCaptureStillimageOutput *imageOutputBack;@property (strong,nonatomic) AVCaptureSession *sessionBack;/* Back camera settings */@property bool isBackRecording;@property (strong,nonatomic) AVCaptureDeviceinput *vIDeoinputFront;@property (strong,nonatomic) AVCaptureStillimageOutput *imageOutputFront;@property (strong,nonatomic) AVCaptureSession *sessionFront;

现在在视图中确实加载了我最初设置了后置摄像头并为会话开始录制设置了标志,或者没有为两个会话设置如下所示.

- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];    [self setupBackAVCapture];    self.isFrontRecording = NO;    self.isBackRecording = NO;}- (voID)setupBackAVCapture{    NSError *error = nil;    self.sessionBack = [[AVCaptureSession alloc] init];    self.sessionBack.sessionPreset = AVCaptureSessionPresetPhoto;    AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVIDeo];    self.vIDeoinputBack = [[AVCaptureDeviceinput alloc] initWithDevice:camera error:&error];    [self.sessionBack addinput:self.vIDeoinputBack];    self.imageOutputBack = [[AVCaptureStillimageOutput alloc] init];    [self.sessionBack addOutput:self.imageOutputBack];}

现在,每当用户开始拍摄照片时,我们将使用下面的代码拍摄前照片.

- (IBAction)buttonCapture:(ID)sender {    [self takeBackPhoto];}- (voID)takeBackPhoto{    [self.sessionBack startRunning];    if (!self.isFrontRecording) {        self.isFrontRecording = YES;        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);        AVCaptureConnection *vIDeoConnection = [self.imageOutputBack connectionWithMediaType:AVMediaTypeVIDeo];        if (vIDeoConnection == nil) {            return;        }        [self.imageOutputBack         captureStillimageAsynchronouslyFromConnection:vIDeoConnection         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer,NSError *error) {             if (imageDataSampleBuffer == NulL) {                 return;             }             NSData *imageData = [AVCaptureStillimageOutput jpegStillimageNSDataRepresentation:imageDataSampleBuffer];             UIImage *image = [[UIImage alloc] initWithData:imageData];             UIImageWritetoSavedPhotosAlbum(image,self,nil,nil);             [self.imageVIEw setimage:image];             [self.sessionBack stopRunning];             // Set up front camera setting and capture photo.             [self setupFrontAVCapture];             [self takeFrontPhoto];         }];        self.isFrontRecording = NO;    }}

一旦捕获了后方图像,我们将使用setupFrontAVCapture方法设置会话以捕获前方图像,然后我们将使用如下所示的takeFrontPhoto方法捕获前方图像.

- (voID)setupFrontAVCapture{    NSError *error = nil;    self.sessionFront = [[AVCaptureSession alloc] init];    self.sessionFront.sessionPreset = AVCaptureSessionPresetPhoto;    AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVIDeo];    camera = [self cameraWithposition:AVCaptureDevicepositionFront];    self.vIDeoinputFront = [[AVCaptureDeviceinput alloc] initWithDevice:camera error:&error];    [self.sessionFront addinput:self.vIDeoinputFront];    self.imageOutputFront = [[AVCaptureStillimageOutput alloc] init];    [self.sessionFront addOutput:self.imageOutputFront];}- (voID)takeFrontPhoto{    [self.sessionFront startRunning];    if (!self.isBackRecording) {        self.isBackRecording = YES;        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);        AVCaptureConnection *vIDeoConnection = [self.imageOutputFront connectionWithMediaType:AVMediaTypeVIDeo];        if (vIDeoConnection == nil) {            return;        }        [self.imageOutputFront         captureStillimageAsynchronouslyFromConnection:vIDeoConnection         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer,nil);             [self.imageVIEwBack setimage:image];             [self.sessionFront stopRunning];         }];        self.isBackRecording = NO;    }}

这样您就可以使用两组不同的AVCaptureSession和AVCaptureStillimageOutput对象来实现您的目标.

如果您有任何疑惑,请告诉我.

总结

以上是内存溢出为你收集整理的ios – 同一AVCaptureSession中的多个AVCaptureVideoDataOutput全部内容,希望文章能够帮你解决ios – 同一AVCaptureSession中的多个AVCaptureVideoDataOutput所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1067786.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存