iOS应用程序视频捕获在光线不足时减慢速度

时间:2013-12-04 20:48:21

标签: ios objective-c camera avcapturesession avcapturedevice

我有一个iOS应用程序正在使用手机的前置摄像头并设置AVCaptureSession来读取传入的摄像头数据。我设置了一个简单的帧计数器来检查数据输入的速度,令我惊讶的是,当相机处于低光照时,帧速率(使用代码中的imagecount变量测量)非常慢,但是一旦我移动手机进入灯火通明的区域,帧速率几乎会增加三倍。我想保持整个图像处理的高帧率,并将minFrameDuration变量设置为30 fps,但这没有帮助。关于为什么这种随机行为的任何想法?

创建捕获会话的代码如下:

#pragma mark Create and configure a capture session and start it running

- (void)setupCaptureSession
{

NSError *error = nil;

// Create the session
session = [[AVCaptureSession alloc] init];

// Configure the session to produce lower resolution video frames, if your
// processing algorithm can cope. We'll specify medium quality for the
// chosen device.
session.sessionPreset = AVCaptureSessionPresetLow;

// Find a suitable AVCaptureDevice
//AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSArray *devices = [AVCaptureDevice devices];
AVCaptureDevice *frontCamera;
AVCaptureDevice *backCamera;

for (AVCaptureDevice *device in devices) {



    if ([device hasMediaType:AVMediaTypeVideo]) {

        if ([device position] == AVCaptureDevicePositionFront) {

            backCamera = device;
        }
        else {

            frontCamera = device;
        }
    }
}


//Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera
                                                                    error:&error];

if (!input) {
    //Handling the error appropriately.
}
[session addInput:input];

// Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];

// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

// Specify the pixel format
output.videoSettings =
[NSDictionary dictionaryWithObject:
 [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                            forKey:(id)kCVPixelBufferPixelFormatTypeKey];

// If you wish to cap the frame rate to a known value, such as 30 fps, set
// minFrameDuration.
output.minFrameDuration = CMTimeMake(1,30);

//Start the session running to start the flow of data

[session startRunning];

}

#pragma mark Delegate routine that is called when a sample buffer was written

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection
{
  //counter to track frame rate
  imagecount++;

  //display to help see speed of images being processed on ios app
  NSString *recognized = [[NSString alloc] initWithFormat:@"IMG COUNT - %d",imagecount];
  [self performSelectorOnMainThread:@selector(debuggingText:) withObject:recognized waitUntilDone:YES];

}

1 个答案:

答案 0 :(得分:3)

当光线较少时,相机需要较长时间曝光才能在每个像素中获得相同的信噪比。这就是为什么你可能会期望帧率在低光下下降。

您正在将minFrameDuration设置为1/30秒,以防止长时间曝光帧降低帧速率。但是,您应该设置maxFrameDuration:您的代码按原样表示帧速率不超过30 FPS,但可能是10 FPS或1 FPS ....

另外,the Documentation表示用lockForConfiguration:和unlockForConfiguration:括起对这些参数的任何更改,所以可能是你的更改没有采取。