woo*_*ekr 9 iphone camera avfoundation autofocus avcapturesession
我使用标准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);
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但有时它有一些问题:
什么想法可能是一个问题?第一个问题肯定更重要.
Ben*_*air 10
老问题但无论如何可能会节省一些时间的挫折感.在呼叫之前设置兴趣点非常重要setFocusMode,否则您的相机会将焦点设置到前一个焦点.可以想象setFocusMode为COMMIT.同样适用于setExposureMode.
Apple的AVCam样本是完全错误和破碎的.
在某些方面,我注意到视频预设比照片预设需要更长的时间来初始化。
\n\n您正在录制视频或拍照吗?
\n\n我注意到您的质量设置为中等,但对于 32BGRA,将捕获模式设置为照片并在捕获后对图像进行下采样可能会更好。还要设置 AVVideoCodecJPEG 而不是 32BGRA。
\n\n[device setOutputSettings:[NSDictionary dictionaryWithObject:AVVideoCodecJPEG forKey:AVVideoCodecKey]];\nRun Code Online (Sandbox Code Playgroud)\n\n代替:
\n\n[device setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];\nRun Code Online (Sandbox Code Playgroud)\n\n如果您在任何时候将焦点模式更改为 AVCaptureFocusModeAutoFocus,您可能还需要注册 \nsubjectAreaChangeMonitoring 通知并强制重新聚焦。
\n\n您可能还需要添加代码来手动设置自动对焦并将其重置为自动,因为有时这是需要的。
\n\n我修改了代码以设置感兴趣的焦点并将相机配置错误输出记录到委托方法。
\n\n- (void)setupCaptureSession { \n NSError *error = nil;\n\n [self setCaptureSession: [[AVCaptureSession alloc] init]]; \n\n self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;\n\n device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];\n\n if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){\n [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];\n if ([device isFocusPointOfInterestSupported])\n [device setFocusPointOfInterest:CGPointMake(0.5f,0.5f)];\n [device unlockForConfiguration];\n }else {\n if ([[self delegate] \n respondsToSelector:@selector(captureManager:didFailWithError:)]) {\n [[self delegate] captureManager:self didFailWithError:error];\n }\n }\n\n AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device \n error:&error];\n if (!input) {\n // TODO: Obs\xc5\x82uga b\xc5\x82\xc4\x99du, gdy nie uda si\xc4\x99 utworzy\xc4\x87 wej\xc5\x9bcia\n }\n [[self captureSession] addInput:input];\n\n AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];\n [[self captureSession] addOutput:output];\n\n dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);\n [output setSampleBufferDelegate:self queue:queue];\n dispatch_release(queue);\n\n output.videoSettings = \n [NSDictionary dictionaryWithObject:\n [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] \n forKey:(id)kCVPixelBufferPixelFormatTypeKey];\n\n\n output.minFrameDuration = CMTimeMake(1, 15);\n\n [[self captureSession] startRunning];\n\n AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];\n captureVideoPreviewLayer.frame = previewLayer.bounds;\n [previewLayer.layer insertSublayer:captureVideoPreviewLayer atIndex:0];\n [previewLayer setHidden:NO];\n\n mutex = YES;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
12399 次 |
| 最近记录: |