想要将摄像头进行视频录制或者拍照可以用UIImagePickerController,不过UIImagePickerController会弹出一个自己的界面,可是有时候我们不想要弹出的这个界面,那么就可以用另一种方法来获取摄像头得到的数据了。
首先需要引入一个包#import <AVFoundation/AVFoundation.h>,接下来你的类需要实现AVCaptureVideoDataOutputSampleBufferDelegate这个协议,只需要实现协议中的一个方法就可以得到摄像头捕获的数据了
- - (void)captureOutput:(AVCaptureOutput *)captureOutput
- didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
- fromConnection:(AVCaptureConnection *)connection
- {
-
- UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
- mData = UIImageJPEGRepresentation(image, 0.5);
-
- }
下面是imageFromSampleBuffer方法,方法经过一系列转换,将CMSampleBufferRef转为UIImage对象,并返回这个对象:
-
- - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
- {
-
- CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
-
- CVPixelBufferLockBaseAddress(imageBuffer, 0);
-
-
- void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
-
-
- size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
-
- size_t width = CVPixelBufferGetWidth(imageBuffer);
- size_t height = CVPixelBufferGetHeight(imageBuffer);
-
-
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
-
-
- CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
- bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
-
- CGImageRef quartzImage = CGBitmapContextCreateImage(context);
-
- CVPixelBufferUnlockBaseAddress(imageBuffer,0);
-
-
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
-
-
-
- UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0f orientation:UIImageOrientationRight];
-
-
- CGImageRelease(quartzImage);
-
- return (image);
- }
不过要想让摄像头工作起来,还得做一些工作才行:
-
- - (void)setupCaptureSession
- {
- NSError *error = nil;
-
-
- AVCaptureSession *session = [[[AVCaptureSession alloc] init] autorelease];
-
-
-
-
- session.sessionPreset = AVCaptureSessionPresetMedium;
-
-
- AVCaptureDevice *device = [AVCaptureDevice
- defaultDeviceWithMediaType:AVMediaTypeVideo];
-
-
- AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
- error:&error];
- if (!input) {
-
- }
- [session addInput:input];
-
-
- AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];
- [session addOutput:output];
-
-
- dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
- [output setSampleBufferDelegate:self queue:queue];
- dispatch_release(queue);
-
-
- output.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
- [NSNumber numberWithInt: 320], (id)kCVPixelBufferWidthKey,
- [NSNumber numberWithInt: 240], (id)kCVPixelBufferHeightKey,
- nil];
-
- AVCaptureVideoPreviewLayer* preLayer = [AVCaptureVideoPreviewLayer layerWithSession: session];
-
- preLayer.frame = CGRectMake(0, 0, 320, 240);
- preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
- [self.view.layer addSublayer:preLayer];
-
-
- output.minFrameDuration = CMTimeMake(1, 15);
-
-
- [session startRunning];
-
-
-
- }
其中preLayer是一个预览摄像的界面,加不加全看自己了,位置什么的也是在preLayer.frame里可设置。这里强调一下output.videoSettings,这里可以配置输出数据的一些配置,比如宽高和视频的格式。你可以在你这个controller中的初始化调用- (void)setupCaptureSession 方法,这样摄像头就开始工作了,这里没有处理关闭什么的,大家可以查文档。
本文转自博客园知识天地的博客,原文链接:,如需转载请自行联系原博主。