gpt4 book ai didi

iphone - 从 AVCaptureSession 捕获图像

转载 作者:行者123 更新时间:2023-12-03 20:21:04 25 4
gpt4 key购买 nike

我正在学习 AVCaptureSession 以及如何使用其委托(delegate)方法捕获多个图像

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection

我的目标是以每秒预定义的速率捕获 1 张或多张图像。例如,每 1 秒 1 或 2 个图像。所以我设置了

 AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
captureOutput.minFrameDuration = CMTimeMake(1, 1);

[self.captureSession startRunning];启动时,我的日志文件显示委托(delegate)每秒被调用20次。它来自哪里以及如何以我想要的间隔捕获图像?

最佳答案

您可以使用下面给出的函数,如果您想以特定的时间间隔进行捕获,则设置一个计时器并再次调用该函数。

-(IBAction)captureNow
{

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [stillImageOutput connections])
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection)
{
break;
}
}

NSLog(@"About to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{

CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
NSLog(@"Attachments: %@", exifAttachments);
}
else
{
NSLog(@"No attachments found.");
}

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[self vImage] setImage:image];

}];
}

更多引用可以查看 iOS4: Take photos with live video preview using AVFoundation .

关于iphone - 从 AVCaptureSession 捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8264749/

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