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

2024-05-08 13:38

本文主要是介绍ios将摄像头捕获的视频数据转为jpeg格式 .,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

// http://blog.csdn.net/yanfangjin/article/details/7456505

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

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

[cpp] view plain copy print ?
  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 print ?
  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 print ?
  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格式 .的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/970476

相关文章

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,