gpt4 book ai didi

iphone - 焦点(自动对焦)在相机中不起作用(AVFoundation AVCaptureSession)

转载 作者:行者123 更新时间:2023-12-03 18:32:39 30 4
gpt4 key购买 nike

我正在使用标准 AVFoundation 类来捕获视频并显示预览 ( http://developer.apple.com/library/ios/#qa/qa1702/_index.html )

这是我的代码:

- (void)setupCaptureSession {       
NSError *error = nil;

[self setCaptureSession: [[AVCaptureSession alloc] init]];

self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;

device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]) {
[device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
[device unlockForConfiguration];
}

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input) {
// TODO: Obsługa błędu, gdy nie uda się utworzyć wejścia
}
[[self captureSession] addInput:input];

AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];
[[self captureSession] addOutput:output];

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

output.videoSettings =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];


output.minFrameDuration = CMTimeMake(1, 15);

[[self captureSession] startRunning];

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
captureVideoPreviewLayer.frame = previewLayer.bounds;
[previewLayer.layer insertSublayer:captureVideoPreviewLayer atIndex:0];
[previewLayer setHidden:NO];

mutex = YES;
}

// Delegate routine that is called when a sample buffer was written
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
if (mutex && ![device isAdjustingFocus] && ![device isAdjustingExposure] && ![device isAdjustingWhiteBalance]) {
// something
}
}

// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer {
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);

// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);

// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);

// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];

// Release the Quartz image
CGImageRelease(quartzImage);

return (image);
}

一切正常,但有时会出现一些问题:

  • 相机对焦不起作用 - 它是随机的,有时有效,有时无效。我在不同的设备上尝试过 iPhone 4 和 3GS。我尝试用谷歌搜索,但没有结果。人们只提到损坏的设备,但我检查了 3 部 iPhone 4 和一部 iPhone 3GS。问题无处不在。
  • 相机加载时间很长。我正在使用 ScannerKit API,出于同样的原因,它也使用相机,并且它的加载速度比我的实现速度快两倍。

有什么想法可能会出现问题吗?第一个问题肯定更重要。

最佳答案

老问题,但无论如何可能会节省一些时间的挫败感。在调用 setFocusMode 之前设置兴趣点非常重要,否则您的相机会将焦点设置到前一个焦点。将 setFocusMode 视为 COMMIT。同样适用于 setExposureMode

Apple 的 AVCam 示例完全错误且已损坏。

关于iphone - 焦点(自动对焦)在相机中不起作用(AVFoundation AVCaptureSession),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391203/

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