1

AVCaptureSession を使用して、前面カメラから画像を取得して処理しようとしています。これまでのところ、新しいフレームが利用可能になるたびに、それを変数に割り当てて、新しいフレームがあるかどうかを 10 分の 1 秒ごとにチェックし、ある場合はそれを処理する NSTimer を実行しました。

フレームを取得し、カメラをフリーズし、いつでも次のフレームを取得したいと考えています。[captureSession getNextFrame] のようなものですね。

これが私のコードの一部ですが、それがどれほど役立つかはわかりません:

- (void)startFeed {

 loopTimerIndex = 0;

    NSArray *captureDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput 
                                          deviceInputWithDevice:[captureDevices objectAtIndex:1] 
                                          error:nil];

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

    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", nil);

    [captureOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);

    NSString *key = (NSString *)kCVPixelBufferPixelFormatTypeKey;
    NSNumber *value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
    [captureOutput setVideoSettings:videoSettings];

    captureSession = [[AVCaptureSession alloc] init];
    captureSession.sessionPreset = AVCaptureSessionPresetLow;
    [captureSession addInput:captureInput];
    [captureSession addOutput:captureOutput];

    imageView = [[UIImage alloc] init];

    [captureSession startRunning];

}

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

 loopTimerIndex++;

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, 0);

    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);

    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);

    imageView = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
    [delegate updatePresentor:imageView];
    if(loopTimerIndex == 1) {
        [delegate feedStarted];
    }

    CGImageRelease(newImage);
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

    [pool drain];

}
4

1 に答える 1

3

フレームを取得するためにカメラを積極的にポーリングすることはありません。これは、キャプチャ プロセスがそのように設計されていないためです。代わりに、フレームを 1/30 秒またはそれより速く表示するのではなく、10 分の 1 秒ごとに表示したい場合は、その間のフレームを無視する必要があります。

-captureOutput:didOutputSampleBuffer:fromConnection:たとえば、トリガーされるたびに比較するタイムスタンプを保持できます。タイムスタンプが現在から 0.1 秒以上の場合、カメラ フレームを処理して表示し、タイムスタンプを現在の時刻にリセットします。それ以外の場合は、フレームを無視します。

于 2010-11-22T20:08:14.407 に答える