1. //
  2. // ViewController.m
  3. // AVFountionCamera
  4. //
  5. // Created by ZhuYi on 16/5/3.
  6. // Copyright © 2016年 DDP. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. #import <AssetsLibrary/AssetsLibrary.h>
  12.  
  13. @interface ViewController (){
  14. //负责输入和输出设置之间的数据传递
  15. AVCaptureSession *captureSesstion;
  16. //负责从AVCaptureDevice获得输入数据
  17. AVCaptureDeviceInput *capturedeviceInput;
  18. //照片输出流
  19. AVCaptureStillImageOutput *captureStillImageOutput;
  20. //相机拍摄预览图层
  21. AVCaptureVideoPreviewLayer *captureVediopreviewLayer;
  22. //拍照按钮
  23. UIButton *takephotoButton;
  24. UIView *viewContainer;
  25. }
  26.  
  27. @end
  28.  
  29. @implementation ViewController
  30.  
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view, typically from a nib.
  34. viewContainer = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - )];
  35. [self.view addSubview:viewContainer];
  36. takephotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
  37. takephotoButton.backgroundColor = [UIColor orangeColor];
  38. [takephotoButton addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
  39. takephotoButton.frame = CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width, );
  40. [self.view addSubview:takephotoButton];
  41. //初始化会话
  42. captureSesstion = [[AVCaptureSession alloc] init];
  43. if ([captureSesstion canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
  44. captureSesstion.sessionPreset = AVCaptureSessionPreset1920x1080;
  45. }
  46. //获得输入设备
  47. AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头
  48. if (!captureDevice) {
  49. NSLog(@"取得后置摄像头时出现问题.");
  50. return;
  51. }
  52. NSError *error=nil;
  53. //根据输入设备初始化设备输入对象,用于获得输入数据
  54. capturedeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
  55. if (error) {
  56. NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription);
  57. return;
  58. }
  59. //初始化设备输出对象,用于获得输出数据
  60. captureStillImageOutput=[[AVCaptureStillImageOutput alloc]init];
  61. NSDictionary *outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG};
  62. [captureStillImageOutput setOutputSettings:outputSettings];//输出设置
  63.  
  64. //将设备输入添加到会话中
  65. if ([captureSesstion canAddInput:capturedeviceInput]) {
  66. [captureSesstion addInput:capturedeviceInput];
  67. }
  68.  
  69. //将设备输出添加到会话中
  70. if ([captureSesstion canAddOutput:captureStillImageOutput]) {
  71. [captureSesstion addOutput:captureStillImageOutput];
  72. }
  73.  
  74. //创建视频预览层,用于实时展示摄像头状态
  75. captureVediopreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSesstion];
  76.  
  77. CALayer *layer = viewContainer.layer;
  78. layer.masksToBounds=YES;
  79.  
  80. captureVediopreviewLayer.frame=layer.bounds;
  81. captureVediopreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式
  82. //将视频预览层添加到界面中
  83. [layer addSublayer:captureVediopreviewLayer];
  84. [captureSesstion startRunning];
  85. }
  86. -(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{
  87. NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  88. for (AVCaptureDevice *camera in cameras) {
  89. if ([camera position]==position) {
  90. return camera;
  91. }
  92. }
  93. return nil;
  94. }
  95. - (void)takePhoto{
  96. //根据设备输出获得连接
  97. AVCaptureConnection *captureConnection=[captureStillImageOutput connectionWithMediaType:AVMediaTypeVideo];
  98. //根据连接取得设备输出的数据
  99. [captureStillImageOutput captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
  100. if (imageDataSampleBuffer) {
  101. NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
  102. UIImage *image=[UIImage imageWithData:imageData];
  103. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
  104. }
  105.  
  106. }];
  107. }
  108. @end

AVFoundation--AVCaptureSession的更多相关文章

  1. &lt;图形图像,动画,多媒体&gt; 读书笔记 --- 录制与编辑视频

    使用UIImagePickerController 进行录制 #import "ViewController.h" #import <MobileCoreServices/M ...

  2. 用AVFoundation自定义相机拍照

    自定义拍照或者录视频的功能,就需要用到AVFoundation框架,目前我只用到了拍照,所以记录下自定义拍照用法,视频用法等用上了再补充,应该是大同小异 demo在这里:https://github. ...

  3. iOS开发--AVFoundation自定义相机

    首先导入一个头文件 #import <AVFoundation/AVFoundation.h> 由于后面我们需要将拍摄好的照片写入系统相册中,所以我们在这里还需要导入一个相册需要的头文件 ...

  4. iOS使用AVFoundation实现二维码扫描(ios7以上)——转载

    关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use ...

  5. iOS使用AVFoundation实现二维码扫描

    原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...

  6. AVFoundation的使用

    AVFoundation的使用 2013-05-03 14:50:21|  分类: iphone_dev_note|举报|字号 订阅         相机相关应用一般会用到AVFoundation. ...

  7. AVFoundation视频流处理

    框架 首先我们从整体对所需框架做个初步了解. AVFoundation在相关框架栈中的的位置: 为了捕捉视频,我们需要这样几种类(与其它的子类). AVCaptureDevice 代表了输入设备,例如 ...

  8. 使用AVCaptureSession捕捉视频

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...

  9. 使用AVCaptureSession捕捉静态图片

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...

  10. 使用AVCaptureSession显示相机预览

    #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ...

随机推荐

  1. C++ 处理 utf-8

    类似"abc汉字"这样的字符串是以utf-8编码; C++ 的 cout执行的操作是把参数发送给stdout,因此如果终端支持utf-8, 汉字可以使用cout打印: 比较好的办法 ...

  2. js中substr,substring,indexOf,lastIndexOf等的用法

    1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...

  3. [SOJ] 简单哈希

    Description 使用链地址法(又称拉链法)可以解决Hash中的冲突问题.其基本思想是:将具有相同哈希地址的记录链成一个单链表,m个哈希地址就设m个单链表,然后用一个数组将m个单链表的表头指针存 ...

  4. 反编译与调试APK

    0×01前言 这年头,apk全都是加密啊,加壳啊,反调试啊,小伙伴们表示已经不能愉快的玩耍了.静态分析越来越不靠谱了,apktool.ApkIDE.jd GUI.dex2jar等已经无法满足大家的需求 ...

  5. Kafka发送消息失败原因

    Kafka发送消息方法如下: Properties properties = new Properties(); properties.put("zookeeper.connect" ...

  6. BIEE应用存储过程并从前台传参

    1.     RPD操作 1.1修改连接池属性 在连接脚本添加SQL,这里选择在查询前执行 新建SQL脚本

  7. Android studio自动删除没用的资源

    有时候我们添加的一些资源,如图片和一些没用的代码,以及在添加第三方库的时候我们只需要使用其中的一部分功能和一部分资源,那么这个时候如果靠我们手工去怕是非常难做的,尤其是项目大的时候,Android 团 ...

  8. 改造jQuery-Tagit 插件支持中文全角的逗号和空格

    jQuery 的 tagit 插件效果还是不错的,今天用到该插件但发现不能自定义标签分隔符,只能是英文半角逗号或空格,于是想改造下 效果: 先研究了一番插件的代码,发现并不能通过插件自身的扩展方法来实 ...

  9. 关于Android平台的搭建的心得---汪永骏

    我本来是.net开发的,但看到目前互联网形式都朝着移动端开发迈进.大势所向,我便也开始学习Android的开发 今天就是要聊一下,我对Android开发的一些心得.今天讲的是,我在搭建Android平 ...

  10. iOS开发系列-九宫格算法-xib

    给大家演示 应用程序下载 小项目,效果图:涉及知识点:懒加载,九宫格算法,字典转模型,自定义UIView ,xib文件的使用 首先把素材拖到Xcode项目中:简单看一下素材文件 此时大家应该首先关注. ...