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

来源:互联网 发布:bjd娃娃正版淘宝店 编辑:程序博客网 时间:2024/06/11 17:15

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

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

[cpp] view plaincopy
  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 plaincopy
  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. }