调用视频流所使用框架:<Foundation/Foundation.h>

必须定义的参数:

1.AVCaptureDevice(捕获设备:前置、后置摄像头等)

2.AVCaptureInput(捕获输入:一般就是捕获设备的输入)

3.AVCaptureOutput(捕获输出:可输入为视频文件、图像文件等)

4.AVCaptureSession(调节多个输入输出)

关键代码:

  1. - (void)setupCamera
  2. {
  3. NSError *error = nil;
  4.  
  5. // Create the session
  6. _session = [[AVCaptureSession alloc] init];
  7.  
  8. // Configure the session to produce lower resolution video frames, if your
  9. // processing algorithm can cope. We'll specify medium quality for the
  10. // chosen device.
  11. _session.sessionPreset = AVCaptureSessionPresetMedium;
  12.  
  13. // Find a suitable AVCaptureDevice
  14. AVCaptureDevice *device = [AVCaptureDevice
  15. defaultDeviceWithMediaType:AVMediaTypeVideo];
  16.  
  17. // Create a device input with the device and add it to the session.
  18. _input = [AVCaptureDeviceInput deviceInputWithDevice:device
  19. error:&error];
  20. if (!_input) {
  21. // Handling the error appropriately.
  22. }
  23. [_session addInput:_input];
  24.  
  25. // Create a VideoDataOutput and add it to the session
  26. _output = [[AVCaptureVideoDataOutput alloc] init];
  27. [_session addOutput:_output];
  28.  
  29. // Configure your output.
  30. dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
  31. [_output setSampleBufferDelegate:self queue:queue];
  32.  
  33. // Specify the pixel format
  34. _output.videoSettings =
  35. [NSDictionary dictionaryWithObject:
  36. [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
  37. forKey:(id)kCVPixelBufferPixelFormatTypeKey];
  38.  
  39. // If you wish to cap the frame rate to a known value, such as 15 fps, set
  40. // minFrameDuration.
  41. _output.minFrameDuration = CMTimeMake(, );
  42.  
  43. // Start the session running to start the flow of data
  44. [_session startRunning];
  45.  
  46. // Assign session to an ivar.
  47. [self setSession:_session];
  48. }
  49.  
  50. // Delegate routine that is called when a sample buffer was written
  51. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  52. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  53. fromConnection:(AVCaptureConnection *)connection
  54. {
  55. // Create a UIImage from the sample buffer data
  56. UIImage *img = [self imageFromSampleBuffer:sampleBuffer];
  57.  
  58. /*
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. self.catchview.image=img;
  61. });
  62. */
  63.  
  64. }
  65. // Create a UIImage from sample buffer data
  66. - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
  67. {
  68.  
  69. // Get a CMSampleBuffer's Core Video image buffer for the media data
  70. CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  71. // Lock the base address of the pixel buffer
  72. CVPixelBufferLockBaseAddress(imageBuffer, );
  73.  
  74. // Get the number of bytes per row for the pixel buffer
  75. size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
  76. // Get the pixel buffer width and height
  77. size_t width = CVPixelBufferGetWidth(imageBuffer);
  78. size_t height = CVPixelBufferGetHeight(imageBuffer);
  79.  
  80. // Get the number of bytes per row for the pixel buffer
  81. u_int8_t *baseAddress = (u_int8_t *)malloc(bytesPerRow*height);
  82. memcpy( baseAddress, CVPixelBufferGetBaseAddress(imageBuffer), bytesPerRow * height );
  83.  
  84. // size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
  85.  
  86. // Create a device-dependent RGB color space
  87. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  88.  
  89. // Create a bitmap graphics context with the sample buffer data
  90.  
  91. //The context draws into a bitmap which is `width'
  92. // pixels wide and `height' pixels high. The number of components for each
  93. // pixel is specified by `space'
  94. CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, ,
  95. bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
  96.  
  97. // Create a Quartz image from the pixel data in the bitmap graphics context
  98. CGImageRef quartzImage = CGBitmapContextCreateImage(context);
  99.  
  100. // Unlock the pixel buffer
  101. CVPixelBufferUnlockBaseAddress(imageBuffer,);
  102.  
  103. // Free up the context and color space
  104. CGContextRelease(context);
  105. //CGColorSpaceRelease(colorSpace);
  106.  
  107. // Create an image object from the Quartz image
  108. UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight];
  109. free(baseAddress);
  110. // Release the Quartz image
  111. CGImageRelease(quartzImage);
  112. return (image);
  113. }

[iOS 视频流开发-获得视频帧处理]的更多相关文章

  1. iOS视频流开发(1)—视频基本概念

    iOS视频流开发(1)-视频基本概念 手机比PC的优势除了便携外,她最重要特点就是可以快速方便的创作多媒体作品.照片分享,语音输入,视频录制,地理位置.一个成功的手机APP从产品形态上都有这其中的一项 ...

  2. iOS视频流开发(2)—视频播放

    承上篇,本篇文章主要介绍iOS视频播放需要用到的类.以及他们的使用场景和开发中遇到的问题. MPMoviePlayerViewController MP简介 iOS提供MPMoviePlayerCon ...

  3. iOS视频流开发(2) — 视频播放

    iOS视频流开发(2) — 视频播放  承上篇,本篇文章主要介绍iOS视频播放需要用到的类.以及他们的使用场景和开发中遇到的问题. MPMoviePlayerViewController MP简介 i ...

  4. iOS开发系列--视频处理

    MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...

  5. iOS开发系列- 视频MPMoviePlayerController

    MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...

  6. FFmpeg进行视频帧提取&音频重采样-Process.waitFor()引发的阻塞超时

    由于产品需要对视频做一系列的解析操作,利用FFmpeg命令来完成视频的音频提取.第一帧提取作为封面图片.音频重采样.字幕压缩等功能: 前一篇文章已经记录了FFmpeg在JAVA中的使用-音频提取&am ...

  7. Python+moviepy音视频剪辑:视频帧数据的本质、Clip的fl方法进行变换处理的原理以及滚屏案例

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  8. 音视频处理基础知识扫盲:数字视频YUV像素表示法以及视频帧和编解码概念介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  9. iOS常用开发资源整理

    在行--专家付费咨询 杂项 App Release Checklist—iOS App发布清单. Hey Focus—帮助你专注于一个任务. Objective Cloud—Objective C A ...

随机推荐

  1. tar 解压缩命令详解

    今天对目录及其文件进行压缩:/usr/local/test# tar -cvf /usr/local/auto_bak/test.tar /usr/local/test 仅打包,不压缩 # tar - ...

  2. Data URI 应用场景小结

    Data URI scheme 在前端开发中是个常用的技术,通常会在 CSS 设置背景图中用到.比如在 Google 的首页就有用到: Data URI scheme 简称 Data URI,经常会被 ...

  3. Validform表单验证总结

    近期项目里用到了表单的验证,选择了Validform_v5.3.2. 先来了解一下一些基本的参数: 通用表单验证方法:Demo: $(".demoform").Validform( ...

  4. (六)观察者模式详解(包含观察者模式JDK的漏洞以及事件驱动模型)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 本章我们讨论一个除前面的单例 ...

  5. WebBrowser与IE的关系,如何设置WebBrowser工作在IE9、10、11模式下?

    Web Browser Control – Specifying the IE Version http://www.west-wind.com/weblog/posts/2011/May/21/We ...

  6. iOS开发小技巧--适当的清空模型中的某个数据,达到自己的需求,记得最后将数据还原(百思项目评论页面处理最热评论)

    一.项目需求,显示所有贴的时候,需要显示最热评论,但是点击进入相应帖子后,最热评论的label不要显示,如图: 解决方案 -- 该暂时保存的暂时保存,该清空的清空                   ...

  7. secureCRT背景颜色设置

    1. Options->Global Options->Advanced select 'Monochrome'  click 'Edit' button normal: Backgrou ...

  8. javaweb写的在线聊天应用

    写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...

  9. cygwin-使用介绍

    cygwin使用: 使用上的方便性很是不错,启动Cygwin以后,会在Windows下得到一个Bash Shell,由于Cygwin是以Windows下的服务运行的,所以很多情况下和在Linux下有很 ...

  10. 【收藏】Android更新UI的几种常见方法

    ----------------将会调用onDraw()重绘控件---------------- 1.view.invalidate刷新UI(主线程)   2.view.postInvalidate刷 ...