[iOS 视频流开发-获得视频帧处理]
调用视频流所使用框架:<Foundation/Foundation.h>
必须定义的参数:
1.AVCaptureDevice(捕获设备:前置、后置摄像头等)
2.AVCaptureInput(捕获输入:一般就是捕获设备的输入)
3.AVCaptureOutput(捕获输出:可输入为视频文件、图像文件等)
4.AVCaptureSession(调节多个输入输出)
关键代码:
- - (void)setupCamera
- {
- NSError *error = nil;
- // Create the session
- _session = [[AVCaptureSession alloc] init];
- // Configure the session to produce lower resolution video frames, if your
- // processing algorithm can cope. We'll specify medium quality for the
- // chosen device.
- _session.sessionPreset = AVCaptureSessionPresetMedium;
- // Find a suitable AVCaptureDevice
- AVCaptureDevice *device = [AVCaptureDevice
- defaultDeviceWithMediaType:AVMediaTypeVideo];
- // Create a device input with the device and add it to the session.
- _input = [AVCaptureDeviceInput deviceInputWithDevice:device
- error:&error];
- if (!_input) {
- // Handling the error appropriately.
- }
- [_session addInput:_input];
- // Create a VideoDataOutput and add it to the session
- _output = [[AVCaptureVideoDataOutput alloc] init];
- [_session addOutput:_output];
- // Configure your output.
- dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
- [_output setSampleBufferDelegate:self queue:queue];
- // Specify the pixel format
- _output.videoSettings =
- [NSDictionary dictionaryWithObject:
- [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
- forKey:(id)kCVPixelBufferPixelFormatTypeKey];
- // If you wish to cap the frame rate to a known value, such as 15 fps, set
- // minFrameDuration.
- _output.minFrameDuration = CMTimeMake(, );
- // Start the session running to start the flow of data
- [_session startRunning];
- // Assign session to an ivar.
- [self setSession:_session];
- }
- // Delegate routine that is called when a sample buffer was written
- - (void)captureOutput:(AVCaptureOutput *)captureOutput
- didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
- fromConnection:(AVCaptureConnection *)connection
- {
- // Create a UIImage from the sample buffer data
- UIImage *img = [self imageFromSampleBuffer:sampleBuffer];
- /*
- dispatch_async(dispatch_get_main_queue(), ^{
- self.catchview.image=img;
- });
- */
- }
- // Create a UIImage from sample buffer data
- - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
- {
- // Get a CMSampleBuffer's Core Video image buffer for the media data
- CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
- // Lock the base address of the pixel buffer
- CVPixelBufferLockBaseAddress(imageBuffer, );
- // Get the number of bytes per row for the pixel buffer
- size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
- // Get the pixel buffer width and height
- size_t width = CVPixelBufferGetWidth(imageBuffer);
- size_t height = CVPixelBufferGetHeight(imageBuffer);
- // Get the number of bytes per row for the pixel buffer
- u_int8_t *baseAddress = (u_int8_t *)malloc(bytesPerRow*height);
- memcpy( baseAddress, CVPixelBufferGetBaseAddress(imageBuffer), bytesPerRow * height );
- // size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
- // Create a device-dependent RGB color space
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- // Create a bitmap graphics context with the sample buffer data
- //The context draws into a bitmap which is `width'
- // pixels wide and `height' pixels high. The number of components for each
- // pixel is specified by `space'
- CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, ,
- bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
- // Create a Quartz image from the pixel data in the bitmap graphics context
- CGImageRef quartzImage = CGBitmapContextCreateImage(context);
- // Unlock the pixel buffer
- CVPixelBufferUnlockBaseAddress(imageBuffer,);
- // Free up the context and color space
- CGContextRelease(context);
- //CGColorSpaceRelease(colorSpace);
- // Create an image object from the Quartz image
- UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight];
- free(baseAddress);
- // Release the Quartz image
- CGImageRelease(quartzImage);
- return (image);
- }
[iOS 视频流开发-获得视频帧处理]的更多相关文章
- iOS视频流开发(1)—视频基本概念
iOS视频流开发(1)-视频基本概念 手机比PC的优势除了便携外,她最重要特点就是可以快速方便的创作多媒体作品.照片分享,语音输入,视频录制,地理位置.一个成功的手机APP从产品形态上都有这其中的一项 ...
- iOS视频流开发(2)—视频播放
承上篇,本篇文章主要介绍iOS视频播放需要用到的类.以及他们的使用场景和开发中遇到的问题. MPMoviePlayerViewController MP简介 iOS提供MPMoviePlayerCon ...
- iOS视频流开发(2) — 视频播放
iOS视频流开发(2) — 视频播放 承上篇,本篇文章主要介绍iOS视频播放需要用到的类.以及他们的使用场景和开发中遇到的问题. MPMoviePlayerViewController MP简介 i ...
- iOS开发系列--视频处理
MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...
- iOS开发系列- 视频MPMoviePlayerController
MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...
- FFmpeg进行视频帧提取&音频重采样-Process.waitFor()引发的阻塞超时
由于产品需要对视频做一系列的解析操作,利用FFmpeg命令来完成视频的音频提取.第一帧提取作为封面图片.音频重采样.字幕压缩等功能: 前一篇文章已经记录了FFmpeg在JAVA中的使用-音频提取&am ...
- Python+moviepy音视频剪辑:视频帧数据的本质、Clip的fl方法进行变换处理的原理以及滚屏案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...
- 音视频处理基础知识扫盲:数字视频YUV像素表示法以及视频帧和编解码概念介绍
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...
- iOS常用开发资源整理
在行--专家付费咨询 杂项 App Release Checklist—iOS App发布清单. Hey Focus—帮助你专注于一个任务. Objective Cloud—Objective C A ...
随机推荐
- tar 解压缩命令详解
今天对目录及其文件进行压缩:/usr/local/test# tar -cvf /usr/local/auto_bak/test.tar /usr/local/test 仅打包,不压缩 # tar - ...
- Data URI 应用场景小结
Data URI scheme 在前端开发中是个常用的技术,通常会在 CSS 设置背景图中用到.比如在 Google 的首页就有用到: Data URI scheme 简称 Data URI,经常会被 ...
- Validform表单验证总结
近期项目里用到了表单的验证,选择了Validform_v5.3.2. 先来了解一下一些基本的参数: 通用表单验证方法:Demo: $(".demoform").Validform( ...
- (六)观察者模式详解(包含观察者模式JDK的漏洞以及事件驱动模型)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 本章我们讨论一个除前面的单例 ...
- WebBrowser与IE的关系,如何设置WebBrowser工作在IE9、10、11模式下?
Web Browser Control – Specifying the IE Version http://www.west-wind.com/weblog/posts/2011/May/21/We ...
- iOS开发小技巧--适当的清空模型中的某个数据,达到自己的需求,记得最后将数据还原(百思项目评论页面处理最热评论)
一.项目需求,显示所有贴的时候,需要显示最热评论,但是点击进入相应帖子后,最热评论的label不要显示,如图: 解决方案 -- 该暂时保存的暂时保存,该清空的清空 ...
- secureCRT背景颜色设置
1. Options->Global Options->Advanced select 'Monochrome' click 'Edit' button normal: Backgrou ...
- javaweb写的在线聊天应用
写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...
- cygwin-使用介绍
cygwin使用: 使用上的方便性很是不错,启动Cygwin以后,会在Windows下得到一个Bash Shell,由于Cygwin是以Windows下的服务运行的,所以很多情况下和在Linux下有很 ...
- 【收藏】Android更新UI的几种常见方法
----------------将会调用onDraw()重绘控件---------------- 1.view.invalidate刷新UI(主线程) 2.view.postInvalidate刷 ...