2

AVCaptureSessionと、デリゲートメソッドを使用して複数の画像をキャプチャする方法について学習しています

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

私の目標は、1秒あたりの事前定義されたレートで1つまたは複数の画像をキャプチャすることです。たとえば、1秒あたり1枚または2枚の画像。だから私は設定しました

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

[self.captureSession startRunning];開始されると、ログファイルにデリゲートが1秒間に20回呼び出されていることが示されます。それはどこから来て、意図した間隔で画像をキャプチャする方法は?

4

2 に答える 2

10

以下の関数を使用できます。特定の間隔でキャプチャする場合は、タイマーを設定して、その関数を再度呼び出します。

-(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:AVFoundationを使用してライブビデオプレビューで写真を撮るをご覧ください。

于 2011-11-25T05:28:29.283 に答える
0

私がしばらく苦労したのは、写真を撮り、キャプチャした画像でUIImageを設定しようとしたときに、大幅な遅延(〜5秒)でした。の中に

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

メソッドでは、UIにリンクされているものなどの通常の関数を使用する[self.image setImage:img]ことはできません。次のように、メインスレッドで実行する必要があります。

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

これが誰かに役立つことを願っています

于 2013-01-09T10:15:01.313 に答える