AVFoundation--AVCaptureSession
// // ViewController.m // AVFountionCamera // // Created by ZhuYi on 16/5/3. // Copyright © 2016年 DDP. All rights reserved. // #import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> @interface ViewController (){ //负责输入和输出设置之间的数据传递 AVCaptureSession *captureSesstion; //负责从AVCaptureDevice获得输入数据 AVCaptureDeviceInput *capturedeviceInput; //照片输出流 AVCaptureStillImageOutput *captureStillImageOutput; //相机拍摄预览图层 AVCaptureVideoPreviewLayer *captureVediopreviewLayer; //拍照按钮 UIButton *takephotoButton; UIView *viewContainer; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. viewContainer = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - )]; [self.view addSubview:viewContainer]; takephotoButton = [UIButton buttonWithType:UIButtonTypeCustom]; takephotoButton.backgroundColor = [UIColor orangeColor]; [takephotoButton addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside]; takephotoButton.frame = CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width, ); [self.view addSubview:takephotoButton]; //初始化会话 captureSesstion = [[AVCaptureSession alloc] init]; if ([captureSesstion canSetSessionPreset:AVCaptureSessionPreset1920x1080]) { captureSesstion.sessionPreset = AVCaptureSessionPreset1920x1080; } //获得输入设备 AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头 if (!captureDevice) { NSLog(@"取得后置摄像头时出现问题."); return; } NSError *error=nil; //根据输入设备初始化设备输入对象,用于获得输入数据 capturedeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error]; if (error) { NSLog(@"取得设备输入对象时出错,错误原因:%@",error.localizedDescription); return; } //初始化设备输出对象,用于获得输出数据 captureStillImageOutput=[[AVCaptureStillImageOutput alloc]init]; NSDictionary *outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG}; [captureStillImageOutput setOutputSettings:outputSettings];//输出设置 //将设备输入添加到会话中 if ([captureSesstion canAddInput:capturedeviceInput]) { [captureSesstion addInput:capturedeviceInput]; } //将设备输出添加到会话中 if ([captureSesstion canAddOutput:captureStillImageOutput]) { [captureSesstion addOutput:captureStillImageOutput]; } //创建视频预览层,用于实时展示摄像头状态 captureVediopreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSesstion]; CALayer *layer = viewContainer.layer; layer.masksToBounds=YES; captureVediopreviewLayer.frame=layer.bounds; captureVediopreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式 //将视频预览层添加到界面中 [layer addSublayer:captureVediopreviewLayer]; [captureSesstion startRunning]; } -(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{ NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *camera in cameras) { if ([camera position]==position) { return camera; } } return nil; } - (void)takePhoto{ //根据设备输出获得连接 AVCaptureConnection *captureConnection=[captureStillImageOutput connectionWithMediaType:AVMediaTypeVideo]; //根据连接取得设备输出的数据 [captureStillImageOutput captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer) { NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image=[UIImage imageWithData:imageData]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); } }]; } @end
AVFoundation--AVCaptureSession的更多相关文章
- <图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频
使用UIImagePickerController 进行录制 #import "ViewController.h" #import <MobileCoreServices/M ...
- 用AVFoundation自定义相机拍照
自定义拍照或者录视频的功能,就需要用到AVFoundation框架,目前我只用到了拍照,所以记录下自定义拍照用法,视频用法等用上了再补充,应该是大同小异 demo在这里:https://github. ...
- iOS开发--AVFoundation自定义相机
首先导入一个头文件 #import <AVFoundation/AVFoundation.h> 由于后面我们需要将拍摄好的照片写入系统相册中,所以我们在这里还需要导入一个相册需要的头文件 ...
- iOS使用AVFoundation实现二维码扫描(ios7以上)——转载
关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use ...
- iOS使用AVFoundation实现二维码扫描
原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...
- AVFoundation的使用
AVFoundation的使用 2013-05-03 14:50:21| 分类: iphone_dev_note|举报|字号 订阅 相机相关应用一般会用到AVFoundation. ...
- AVFoundation视频流处理
框架 首先我们从整体对所需框架做个初步了解. AVFoundation在相关框架栈中的的位置: 为了捕捉视频,我们需要这样几种类(与其它的子类). AVCaptureDevice 代表了输入设备,例如 ...
- 使用AVCaptureSession捕捉视频
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...
- 使用AVCaptureSession捕捉静态图片
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/ ...
- 使用AVCaptureSession显示相机预览
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ...
随机推荐
- JS 多个参数的传递
var link = $("<a/>", { href: '/StandardManagement/StandardActivitiesDetail?' + $.par ...
- SDN理解:云数据中心底层网络架构
目录 - 目录 - 云数据中心流量类型 - NSX整体网络结构 - 管理网络(API网络) - 租户网络 - 外联网络 - 存储网络 - openstack整体网络结构 - 管理网络:(上图中蓝线) ...
- OpenCV备忘
都是转来的内容的,算是整理一下 OpenCV备忘 深度和通道的理解 CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵 CV_8UC1 CV_8SC1 ...
- DOM学习笔记--入门1
HTML DOM 是关于如何获取.修改.添加或删除 HTML 元素的标准. 首先节点有很多种,不仅仅HTML元素是节点,尤其 要注意文本节点的存在. 根据 W3C 的 HTML DOM 标准,HTML ...
- 图片添加border 不占用图片的大小
因为设计稿中的分割线大多分为两种情况:1.在图片右侧:2.在图片右侧+下方. 那么使用伪类before和after以及绝对定位很容易在不改变原布局的情况使图片按照设计稿输出和保留分割线. 例div.i ...
- android应用编译失败 ResXMLTree_node size 类错误,以及 android studio 项目内搜索
今天很郁闷,又遇到个很让人崩溃的问题: ResXMLTree_node size 0 is smaller than header size 0x45. 类似这样的错误,提示中看不出任何有用的内容,网 ...
- CodeForces 645D Robot Rapping Results Report
二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...
- CodeForces 670B Game of Robots
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- Android如何查看应用签名信息
转自http://www.trinea.cn/android/android-view-signatures/comment-page-1/ 介绍Android如何查看自己的应用签名及三方APK或系统 ...
- 一些Android经验
1.如果在调试Android程序中,你非常确定你的代码是没有问题的,比如在跟Server交互时候,抓包软件抓到的包是正常的,但是在解析数据时候有问题, 你可以试着换个Android设备看看,模拟器换成 ...