AVCaptureSession拍照时如何设置扫描速率

本文探讨了如何使用AVCaptureSession及其代理方法捕获单张或多张图片,并介绍了如何通过设置来实现每秒捕获一张或几张图片的目标。文中提供了一个示例函数,用于按特定间隔捕获图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://stackoverflow.com/questions/8264749/capturing-images-from-avcapturesession

I am learning about AVCaptureSession and how to capture multiple images with its delegate method

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

My goal is to capture 1 or many images with a predefined rate per second. For example, 1 or 2 images per 1 second. So I set

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

When [self.captureSession startRunning]; is started my log file shows delegate is being called 20 times a second. Where is it coming from and how to capture images with my intended intervals?

share | improve this question
  add comment

2 Answers

up vote 4 down vote accepted

You can use the function given below and if you want to capture at specific intervals, then set a timer and call that function again.

-(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(@"attachements: %@", exifAttachments);
        }
        else 
            NSLog(@"no attachments");

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

        self.vImage.image = image;
    }];
}

For more reference you can see iOS4: Take photos with live video preview using AVFoundation.

share | improve this answer
 
 
Very helpful! Thanks a lot. –   Vad  Nov 25 '11 at 18:43
add comment

Something that I struggled with for a while was a massive delay (~5 sec) when taking a picture, and trying to set a UIImage with the captured image. in the

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

method, you cant use normal functions such as [self.image setImage:img] for things that are linked to the UI, you have to run them on the main thread like so:

 [self.image performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:TRUE];

Hope this helps someone


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值