gpt4 book ai didi

objective-c - 在 Mac 上使用 AVFoundation 捕获 iSight 图像

转载 作者:太空狗 更新时间:2023-10-30 03:36:38 27 4
gpt4 key购买 nike

我之前使用此代码使用 QTKit 从 Mac 的 iSight 摄像头捕获单个图像:

- (NSError*)takePicture
{
BOOL success;
NSError* error;

captureSession = [QTCaptureSession new];
QTCaptureDevice* device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];

success = [device open: &error];
if (!success) { return error; }

QTCaptureDeviceInput* captureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice: device];
success = [captureSession addInput: captureDeviceInput error: &error];
if (!success) { return error; }

QTCaptureDecompressedVideoOutput* captureVideoOutput = [QTCaptureDecompressedVideoOutput new];
[captureVideoOutput setDelegate: self];

success = [captureSession addOutput: captureVideoOutput error: &error];
if (!success) { return error; }

[captureSession startRunning];
return nil;
}
- (void)captureOutput: (QTCaptureOutput*)captureOutput
didOutputVideoFrame: (CVImageBufferRef)imageBuffer
withSampleBuffer: (QTSampleBuffer*)sampleBuffer
fromConnection: (QTCaptureConnection*)connection
{
CVBufferRetain(imageBuffer);

if (imageBuffer) {
[captureSession removeOutput: captureOutput];
[captureSession stopRunning];

NSCIImageRep* imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: imageBuffer]];

_result = [[NSImage alloc] initWithSize: [imageRep size]];
[_result addRepresentation: imageRep];

CVBufferRelease(imageBuffer);

_done = YES;
}
}

但是,我今天发现 QTKit 已被弃用,因此我们现在必须使用 AVFoundation。谁能帮我把这段代码转换成它的 AVFoundation 等价物?似乎许多方法都具有相同的名称,但与此同时,很多方法都不同,我在这里完全不知所措......有什么帮助吗?

最佳答案

好的,我找到了解决方案!!在这里:

- (void)takePicture
{
NSError* error;
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice: device error: &error];
if (!input) {
_error = error;
_done = YES;
return;
}

AVCaptureStillImageOutput* output = [AVCaptureStillImageOutput new];
[output setOutputSettings: @{(id)kCVPixelBufferPixelFormatTypeKey: @(k32BGRAPixelFormat)}];

captureSession = [AVCaptureSession new];
captureSession.sessionPreset = AVCaptureSessionPresetPhoto;

[captureSession addInput: input];
[captureSession addOutput: output];
[captureSession startRunning];

AVCaptureConnection* connection = [output connectionWithMediaType: AVMediaTypeVideo];
[output captureStillImageAsynchronouslyFromConnection: connection completionHandler: ^(CMSampleBufferRef sampleBuffer, NSError* error) {
if (error) {
_error = error;
_result = nil;
}
else {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

if (imageBuffer) {
CVBufferRetain(imageBuffer);

NSCIImageRep* imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: imageBuffer]];

_result = [[NSImage alloc] initWithSize: [imageRep size]];
[_result addRepresentation: imageRep];

CVBufferRelease(imageBuffer);
}
}

_done = YES;
}];
}

我希望这能帮助那些在做同样的事情时遇到任何问题的人。

关于objective-c - 在 Mac 上使用 AVFoundation 捕获 iSight 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048230/

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