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 方法,这样摄像头就开始工作了,这里没有处理关闭什么的,大家可以查文档。

本文转自博客园知识天地的博客,原文链接:ios将摄像头捕获的视频数据转为jpeg格式,如需转载请自行联系原博主。


相关文章
|
3天前
|
IDE 开发工具 Android开发
安卓与iOS开发对比:平台选择对项目成功的影响
【9月更文挑战第10天】在移动应用开发的世界中,选择正确的平台是至关重要的。本文将深入探讨安卓和iOS这两大主要移动操作系统的开发环境,通过比较它们的市场份额、开发工具、编程语言和用户群体等方面,为开发者提供一个清晰的指南。我们将分析这两个平台的优势和劣势,并讨论如何根据项目需求和目标受众来做出最佳选择。无论你是初学者还是有经验的开发者,这篇文章都将帮助你更好地理解每个平台的特性,并指导你做出明智的决策。
|
1天前
|
API Android开发 iOS开发
安卓与iOS开发中的线程管理对比
【9月更文挑战第12天】在移动应用的世界中,安卓和iOS平台各自拥有庞大的用户群体。开发者们在这两个平台上构建应用时,线程管理是他们必须面对的关键挑战之一。本文将深入探讨两大平台在线程管理方面的异同,通过直观的代码示例,揭示它们各自的设计理念和实现方式,帮助读者更好地理解如何在安卓与iOS开发中高效地处理多线程任务。
|
3天前
|
开发框架 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的指南
在移动应用开发的广阔天地中,安卓与iOS两大平台各占半壁江山。本文将深入浅出地对比这两大操作系统的开发环境、工具和用户体验设计,揭示它们在编程语言、开发工具以及市场定位上的根本差异。我们将从开发者的视角出发,逐步剖析如何根据项目需求和目标受众选择适合的平台,同时探讨跨平台开发框架的利与弊,为那些立志于打造下一个热门应用的开发者提供一份实用的指南。
14 5
|
3天前
|
开发工具 Android开发 iOS开发
安卓与iOS开发:平台选择的艺术与科学
在移动应用开发的广阔天地中,安卓与iOS两大平台如同东西方哲学的碰撞,既有共通之处又各具特色。本文将深入探讨这两个平台的设计理念、开发工具和市场定位,旨在为开发者提供一份简明扼要的指南,帮助他们在这场技术与商业的博弈中找到自己的道路。通过比较分析,我们将揭示每个平台的优势与局限,以及它们如何影响应用的性能、用户体验和市场接受度。无论你是初涉江湖的新手,还是经验丰富的老手,这篇文章都将为你的选择提供新的视角和思考。
16 5
|
3天前
|
开发工具 Android开发 Swift
探索安卓与iOS开发的差异:从新手到专家的旅程
在数字时代的浪潮中,移动应用开发已成为连接世界的桥梁。本文将深入探讨安卓与iOS这两大主流平台的开发差异,带领读者从零基础出发,逐步了解各自的特点、开发环境、编程语言及市场策略。无论你是梦想成为移动应用开发者的初学者,还是希望扩展技能边界的资深开发者,这篇文章都将为你提供宝贵的见解和实用的建议。
|
4天前
|
人工智能 Android开发 iOS开发
安卓与iOS开发:平台选择的艺术
在移动应用开发的广阔天地里,安卓和iOS两大操作系统各占半壁江山。本文将深入探讨这两个平台的开发环境、工具及市场趋势,帮助开发者在选择适合自己项目的平台时做出更明智的决策。通过比较各自的优势与局限,我们不仅能更好地理解每个系统的核心特性,还能洞察未来技术发展的脉络。无论你是刚入行的新手还是资深开发者,这篇文章都将为你提供有价值的参考和启示。
18 5
|
4天前
|
开发工具 Android开发 iOS开发
安卓与iOS开发:一场操作系统的较量
在数字时代的浪潮中,安卓和iOS这两大操作系统如同海上的两艘巨轮,各自承载着不同的使命与梦想。本文将深入浅出地探讨这两个系统在开发领域的异同,从用户体验、开发工具、市场趋势等多个维度进行比较分析。通过这场技术的较量,我们可以更好地理解每个系统的优势与局限,以及它们如何影响我们的日常生活和工作。
|
3天前
|
Linux Android开发 iOS开发
探索Android与iOS开发:平台之战还是互补共生?
在移动应用开发的浩瀚宇宙中,Android和iOS这两大星系始终吸引着无数开发者的目光。它们各自拥有独特的引力场,引领着技术潮流的方向。本文将穿梭于这两个平台的星际空间,揭示它们背后的力量对比,以及如何在这两者之间找到平衡点,共同推动移动应用开发的进步。
13 1
|
3天前
|
移动开发 开发框架 Android开发
安卓与iOS开发:平台之战的新篇章
在移动应用开发的广阔天地中,安卓和iOS始终占据着主导地位。本文通过比较这两个平台的发展历程、技术特点及未来趋势,探讨了它们之间的竞争与合作。文章旨在为开发者提供一个清晰的平台选择指南,并预测未来移动开发的可能走向。
11 1