ios将摄像头捕获的视频数据转为jpeg格式

想要将摄像头进行视频录制或者拍照可以用UIImagePickerController,不过UIImagePickerController会弹出一个自己的界面,可是有时候我们不想要弹出的这个界面,那么就可以用另一种方法来获取摄像头得到的数据了。

首先需要引入一个包#import <AVFoundation/AVFoundation.h>,接下来你的类需要实现AVCaptureVideoDataOutputSampleBufferDelegate这个协议,只需要实现协议中的一个方法就可以得到摄像头捕获的数据了

 

[cpp]  view plain copy
  1. - (void)captureOutput:(AVCaptureOutput *)captureOutput   
  2. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer   
  3.        fromConnection:(AVCaptureConnection *)connection  
  4. {   
  5.     // Create a UIImage from the sample buffer data  
  6.     UIImage *image = [self imageFromSampleBuffer:sampleBuffer];  
  7.     mData = UIImageJPEGRepresentation(image, 0.5);//这里的mData是NSData对象,后面的0.5代表生成的图片质量  
  8.       
  9. }  


下面是imageFromSampleBuffer方法,方法经过一系列转换,将CMSampleBufferRef转为UIImage对象,并返回这个对象:

 

 

[cpp]  view plain copy
  1. // Create a UIImage from sample buffer data  
  2. - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer   
  3. {  
  4.     // Get a CMSampleBuffer's Core Video image buffer for the media data  
  5.     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);   
  6.     // Lock the base address of the pixel buffer  
  7.     CVPixelBufferLockBaseAddress(imageBuffer, 0);   
  8.       
  9.     // Get the number of bytes per row for the pixel buffer  
  10.     void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);   
  11.       
  12.     // Get the number of bytes per row for the pixel buffer  
  13.     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);   
  14.     // Get the pixel buffer width and height  
  15.     size_t width = CVPixelBufferGetWidth(imageBuffer);   
  16.     size_t height = CVPixelBufferGetHeight(imageBuffer);   
  17.       
  18.     // Create a device-dependent RGB color space  
  19.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();   
  20.       
  21.     // Create a bitmap graphics context with the sample buffer data  
  22.     CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,   
  23.                                                  bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);   
  24.     // Create a Quartz image from the pixel data in the bitmap graphics context  
  25.     CGImageRef quartzImage = CGBitmapContextCreateImage(context);   
  26.     // Unlock the pixel buffer  
  27.     CVPixelBufferUnlockBaseAddress(imageBuffer,0);  
  28.       
  29.     // Free up the context and color space  
  30.     CGContextRelease(context);   
  31.     CGColorSpaceRelease(colorSpace);  
  32.       
  33.     // Create an image object from the Quartz image  
  34.     //UIImage *image = [UIImage imageWithCGImage:quartzImage];  
  35.     UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0f orientation:UIImageOrientationRight];  
  36.       
  37.     // Release the Quartz image  
  38.     CGImageRelease(quartzImage);  
  39.       
  40.     return (image);  
  41. }     

 

不过要想让摄像头工作起来,还得做一些工作才行:

 

[cpp]  view plain copy
  1. // Create and configure a capture session and start it running  
  2. - (void)setupCaptureSession   
  3. {  
  4.     NSError *error = nil;  
  5.       
  6.     // Create the session  
  7.     AVCaptureSession *session = [[[AVCaptureSession alloc] init] autorelease];  
  8.       
  9.     // Configure the session to produce lower resolution video frames, if your   
  10.     // processing algorithm can cope. We'll specify medium quality for the  
  11.     // chosen device.  
  12.     session.sessionPreset = AVCaptureSessionPresetMedium;  
  13.       
  14.     // Find a suitable AVCaptureDevice  
  15.     AVCaptureDevice *device = [AVCaptureDevice  
  16.                                defaultDeviceWithMediaType:AVMediaTypeVideo];//这里默认是使用后置摄像头,你可以改成前置摄像头  
  17.       
  18.     // Create a device input with the device and add it to the session.  
  19.     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device   
  20.                                                                         error:&error];  
  21.     if (!input) {  
  22.         // Handling the error appropriately.  
  23.     }  
  24.     [session addInput:input];  
  25.       
  26.     // Create a VideoDataOutput and add it to the session  
  27.     AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];  
  28.     [session addOutput:output];  
  29.       
  30.     // Configure your output.  
  31.     dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);  
  32.     [output setSampleBufferDelegate:self queue:queue];  
  33.     dispatch_release(queue);  
  34.       
  35.     // Specify the pixel format  
  36.     output.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:  
  37.                               [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,  
  38.                               [NSNumber numberWithInt: 320], (id)kCVPixelBufferWidthKey,  
  39.                               [NSNumber numberWithInt: 240], (id)kCVPixelBufferHeightKey,  
  40.                               nil];  
  41.       
  42.     AVCaptureVideoPreviewLayer* preLayer = [AVCaptureVideoPreviewLayer layerWithSession: session];  
  43.     //preLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];  
  44.     preLayer.frame = CGRectMake(0, 0, 320, 240);  
  45.     preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;    
  46.     [self.view.layer addSublayer:preLayer];  
  47.     // If you wish to cap the frame rate to a known value, such as 15 fps, set   
  48.     // minFrameDuration.  
  49.     output.minFrameDuration = CMTimeMake(1, 15);  
  50.       
  51.     // Start the session running to start the flow of data  
  52.     [session startRunning];  
  53.       
  54.     // Assign session to an ivar.  
  55.     //[self setSession:session];  
  56. }  


其中preLayer是一个预览摄像的界面,加不加全看自己了,位置什么的也是在preLayer.frame里可设置。这里强调一下output.videoSettings,这里可以配置输出数据的一些配置,比如宽高和视频的格式。你可以在你这个controller中的初始化调用- (void)setupCaptureSession 方法,这样摄像头就开始工作了,这里没有处理关闭什么的,大家可以查文档。

 

只能在真机中调试,模拟器不可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值