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格式,如需转载请自行联系原博主。


相关文章
|
2天前
|
存储 定位技术 Swift
探索iOS开发:从新手到专家
【8月更文挑战第29天】在这篇指南中,我们将一起踏上iOS开发的旅程。无论你是刚入门的新手,还是希望提升技能的开发者,本文将为你提供一条清晰的道路。我们将从基础概念讲起,逐步深入到高级技巧,确保你能够掌握iOS开发的核心知识。准备好了吗?让我们开始吧!
|
10天前
|
安全 网络安全 Android开发
安卓与iOS开发:选择的艺术网络安全与信息安全:漏洞、加密与意识的交织
【8月更文挑战第20天】在数字时代,安卓和iOS两大平台如同两座巍峨的山峰,分别占据着移动互联网的半壁江山。它们各自拥有独特的魅力和优势,吸引着无数开发者投身其中。本文将探讨这两个平台的特点、优势以及它们在移动应用开发中的地位,帮助读者更好地理解这两个平台的差异,并为那些正在面临选择的开发者提供一些启示。
110 56
|
1天前
|
开发工具 C语言 Swift
探索iOS开发之旅:从入门到精通
【8月更文挑战第30天】在这篇文章中,我们将一起踏上一场关于iOS开发的奇妙旅程。无论你是刚刚接触iOS开发的新手,还是希望提升自己技能的开发者,这篇文章都将为你提供有价值的指导和启示。我们将从基础的iOS开发概念开始,逐步深入到高级技巧和最佳实践。通过这篇文章,你将了解到如何构建一个成功的iOS应用程序,以及如何不断提升自己的开发技能。让我们一起开启这场探索之旅吧!
10 4
|
1天前
|
Swift iOS开发 开发者
探索iOS开发:SwiftUI的魔力
【8月更文挑战第30天】在这篇文章中,我们将一起揭开SwiftUI的神秘面纱,这是Apple为iOS开发者带来的一种创新和简化界面设计的框架。通过直观易懂的语言和实际案例,我们会深入探讨SwiftUI如何让代码变得像诗一样优美,同时提升开发效率。你将看到,即便是编程初学者也能迅速上手,构建出令人惊叹的应用界面。让我们跟随SwiftUI的步伐,开启一段高效、愉悦的开发旅程。
|
1天前
|
设计模式 Swift iOS开发
探索iOS开发之旅:从新手到专家
【8月更文挑战第30天】本文将引导你进入iOS开发的奇妙世界。无论你是编程新手,还是希望提升你的iOS开发技能的开发者,这篇文章都会为你提供有价值的信息和建议。我们将从基础的iOS开发概念开始,然后逐步深入到更复杂的主题,如设计模式、性能优化和最新技术趋势。最后,我们会提供一些实用的代码示例,帮助你更好地理解和实践所学知识。让我们一起踏上这段激动人心的旅程吧!
|
2天前
|
设计模式 Java Android开发
探索安卓应用开发:从新手到专家的旅程探索iOS开发中的SwiftUI框架
【8月更文挑战第29天】本文旨在通过一个易于理解的旅程比喻,带领读者深入探讨安卓应用开发的各个方面。我们将从基础概念入手,逐步过渡到高级技术,最后讨论如何维护和推广你的应用。无论你是编程新手还是有经验的开发者,这篇文章都将为你提供有价值的见解和实用的代码示例。让我们一起开始这段激动人心的旅程吧!
|
3天前
|
移动开发 搜索推荐 Android开发
安卓与iOS开发:一场跨平台的技术角逐
在移动开发的广阔舞台上,两大主角——安卓和iOS,持续上演着激烈的技术角逐。本文将深入浅出地探讨这两个平台的开发环境、工具和未来趋势,旨在为开发者揭示跨平台开发的秘密,同时激发读者对技术进步的思考和对未来的期待。
|
6天前
|
安全 Android开发 Swift
安卓与iOS开发:平台差异与技术选择
【8月更文挑战第26天】 在移动应用开发的广阔天地中,安卓和iOS两大平台各占一方。本文旨在探索这两个系统在开发过程中的不同之处,并分析开发者如何根据项目需求选择合适的技术栈。通过深入浅出的对比,我们将揭示各自平台的优势与挑战,帮助开发者做出更明智的决策。
|
2天前
|
存储 Swift iOS开发
探索iOS开发中的SwiftUI框架
【8月更文挑战第29天】本文旨在引导读者理解并掌握SwiftUI框架,通过简明的语言和直观的代码示例,揭示SwiftUI如何让iOS开发变得更加简单高效。文章不仅介绍基础知识,更深入探讨了SwiftUI的核心特性、布局技巧以及与UIKit的差异,为开发者提供实用的学习路径和实践指南。
|
9天前
|
IDE 开发工具 Android开发
探索安卓与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚,引领着技术进步的潮流。本文旨在深入剖析这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计以及市场分布等方面。通过对比分析,我们不仅能更好地理解每个平台的独特优势,还能洞察这些差异如何影响项目决策和最终成果。无论你是开发者还是企业决策者,了解这些内容都将助你一臂之力,在选择适合自己项目的开发平台时做出更明智的决策。
下一篇
云函数