【问题标题】:Capturing Images from AVCaptureSession从 AVCaptureSession 捕获图像
【发布时间】:2012-01-06 01:50:28
【问题描述】:

我正在学习 AVCaptureSession 以及如何使用它的委托方法捕获多个图像

- (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]; 启动时,我的日志文件显示委托每秒被调用 20 次。它来自哪里以及如何以我的预期间隔捕获图像?

【问题讨论】:

    标签: iphone objective-c uiimageview calayer avcapturesession


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我一直在努力解决的问题是拍照时出现巨大延迟(约 5 秒),并尝试使用捕获的图像设置 UIImage。在

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

      方法,你不能使用[self.image setImage:img]之类的普通函数来链接到UI的东西,你必须像这样在主线程上运行它们:

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

      希望这对某人有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 2010-12-21
        • 2023-03-21
        相关资源
        最近更新 更多