我的当前设置如下(基于来自Brad Larson的ColorTrackingCamera项目):
我正在使用一个AVCaptureSession
,其设置为AVCaptureSessionPreset640x480
,通过OpenGL场景作为纹理运行输出。然后由片段着色器对此纹理进行操作。
我需要这种“较低质量”的预设,因为我想在用户预览时保持高帧率。然后,当用户拍摄静态照片时,我想切换到更高质量的输出。
起初,我认为可以更改AVCaptureSession
上的sessionPreset
,但这会强制相机重新对焦,从而破坏了可用性。
[captureSession beginConfiguration];
captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
[captureSession commitConfiguration];
目前我正在尝试向AVCaptureSession添加第二个AVCaptureStillImageOutput
,但是我得到了一个空的像素缓冲区,所以我认为我有点卡住了。
这是我的会话设置代码:
...
// Add the video frame output
[captureSession beginConfiguration];
videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setAlwaysDiscardsLateVideoFrames:YES];
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
if ([captureSession canAddOutput:videoOutput])
{
[captureSession addOutput:videoOutput];
}
else
{
NSLog(@"Couldn't add video output");
}
[captureSession commitConfiguration];
// Add still output
[captureSession beginConfiguration];
stillOutput = [[AVCaptureStillImageOutput alloc] init];
if([captureSession canAddOutput:stillOutput])
{
[captureSession addOutput:stillOutput];
}
else
{
NSLog(@"Couldn't add still output");
}
[captureSession commitConfiguration];
// Start capturing
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];
if(![captureSession isRunning])
{
[captureSession startRunning];
};
...
这是我的截取方法:
- (void)prepareForHighResolutionOutput
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
[stillOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:
^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
int width = CVPixelBufferGetWidth(pixelBuffer);
int height = CVPixelBufferGetHeight(pixelBuffer);
NSLog(@"%i x %i", width, height);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
}];
}
(width
和height
都为0)
我已经阅读了AVFoundation文档,但似乎我没有掌握一些重要的内容。
AVCaptureStillImageOutput
预设,但我必须明确设置outputSettings
以避免颜色空间之间的转换。我会立即将其发布为答案。 - polyclick