gpt4 book ai didi

iphone - 拍摄照片时切换 AVCaptureSession 预设

转载 作者:可可西里 更新时间:2023-11-01 03:17:54 26 4
gpt4 key购买 nike

我当前的设置如下(基于 Brad Larson 的 ColorTrackingCamera 项目):

我正在使用设置为 AVCaptureSessionPreset640x480AVCaptureSession,我让输出作为纹理通过 OpenGL 场景运行。然后,该纹理由片段着色器处理。

我需要这个“低质量”预设,因为我想在用户预览时保持高帧率。然后我想在用户拍摄静态照片时切换到更高质量的输出。

首先,我想我可以更改 AVCaptureSession 上的 sessionPreset,但这会强制相机重新聚焦,从而破坏可用性。

[captureSession beginConfiguration];
captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
[captureSession commitConfiguration];

目前我正在尝试向 AVCaptureSession 添加第二个 AVCaptureStillImageOutput,但我得到一个空的像素缓冲区,所以我觉得我有点卡住了。

这是我的 session 设置代码:

...

// 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);
}];
}

(宽度高度结果为0)

我已经通读了 AVFoundation 文档的文档,但似乎没有得到必要的东西。

最佳答案

我找到了针对我的特定问题的解决方案。如果有人遇到同样的问题,我希望它可以作为指南。

帧率大幅下降的原因与像素格式之间的内部转换有关。明确设置像素格式后,帧率增加。

在我的情况下,我使用以下方法创建 BGRA 纹理:

// Let Core Video create the OpenGL texture from pixelbuffer
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, videoTextureCache, pixelBuffer, NULL,
GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA,
GL_UNSIGNED_BYTE, 0, &videoTexture);

因此,当我设置 AVCaptureStillImageOutput 实例时,我将代码更改为:

// Add still output
stillOutput = [[AVCaptureStillImageOutput alloc] init];
[stillOutput setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];

if([captureSession canAddOutput:stillOutput])
{
[captureSession addOutput:stillOutput];
}
else
{
NSLog(@"Couldn't add still output");
}

我希望有一天这对某人有所帮助;)

关于iphone - 拍摄照片时切换 AVCaptureSession 预设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com