iOS 相机开发总结
# Camera 组件讲解
IOS相机
## UIImagePickerController
1.IOS中扑捉照片和视频最简单的方法是使用UIImagePickerController,一个封装了完整视频捕获管线和相机 UI 的 view controller。
2.UIImagePickerController通过cameraOverlayView自定义UI(需要绑定事件)
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
-(void)setupCameraViewController {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *availableMediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
NSLog(@"availableMediaTypes = %@",availableMediaTypes);
if ([availableMediaTypes containsObject:(NSString *)kUTTypeMovie]) {
// 支持视频录制
UIImagePickerController *cameraViewController = [[UIImagePickerController alloc] init];
cameraViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraViewController.mediaTypes = @[(NSString *)kUTTypeMovie]; // 设置相机的媒体类型 (图片 视频 livephoto)
cameraViewController.delegate = self;
cameraViewController.videoQuality = UIImagePickerControllerQualityTypeHigh; // 设置录制视频的质量
UIView *cameraOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 68)];
cameraOverlay.backgroundColor = [UIColor redColor];
cameraViewController.showsCameraControls = NO;
cameraViewController.cameraOverlayView = cameraOverlay;
[self presentViewController:cameraViewController animated:YES completion:nil];
}
}
}
## AVFoundation
AVFoundation提供了比UIImagePickerController更多的关于处理捕获视频、图片的方法。
### AVFoundation相关类
AVFoundation 框架基于以下几个类实现图像、视频的捕捉 ,通过这些类可以访问来自相机设备的原始数据并控制它的组件。
***AVCaptureDevice*** 是关于相机硬件的接口。它被用于控制硬件特性,诸如镜头的位置、曝光、闪光灯等。
***AVCaptureDeviceInput*** 提供来自设备的数据。
***AVCaptureOutput*** 是一个抽象类,描述 capture session 的结果。以下是三种关于静态图片捕捉的具体子类:
AVCaptureStillImageOutput 用于捕捉静态图片
AVCaptureMetadataOutput 启用检测人脸和二维码
AVCaptureVideoOutput 为实时预览图提供原始帧
***AVCaptureSession*** 管理输入与输出之间的数据流,以及在出现问题时生成运行时错误。
***AVCaptureVideoPreviewLayer*** 是 CALayer 的子类,可被用于自动显示相机产生的实时图像。它还有几个工具性质的方法,可将 layer 上的坐标转化到设备上。它看起来像输出,但其实不是。另外,它拥有 session (outputs 被 session 所拥有)。
### 初始化相机并拍照片的步骤
#### AVCaptureSession
AVFoundation 中关于视频捕获的主要的类是 AVCaptureSession。
1. AVCaptureSession 它负责调配影音输入与输出之间的数据流:
2. AVCaptureDevice AVCaptureDeviceInput(设备、设备输入源)
3. 显示图像流方法一:AVCaptureVideoPreviewLayer(显示相机的图像流,so easy!!!)
没这么简单,大头再下面
4. 显示图像流方法二:通过AVCaptureVideoDataOutput从输出数据流捕捉单一的图像帧,并使用 OpenGL 手动地把它们显示在 view(GLKView) 上。
特效滤镜(美颜、美型、颜色等实时处理的滤镜) 粒子
5. AVCaptureStillImageOutput捕捉静态图
- (void)setupCamera {
//1. 创建AVCaptureSession队形管理输入与输出之间的数据流
_captureSession = [[AVCaptureSession alloc] init];
// 2. 获取想要的输入设备 (摄像头 麦克风)
__block AVCaptureDevice *frontDevice;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
[devices enumerateObjectsUsingBlock:^(AVCaptureDevice *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.position == AVCaptureDevicePositionFront) {
frontDevice = obj;
*stop = YES;
}
}];
// 3. 根据想要的输入设备创建输入源,然后交给_captureSession
if (frontDevice) {
AVCaptureDeviceInput *frontDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:frontDevice error:nil];
if (frontDeviceInput) {
if ([_captureSession canAddInput:frontDeviceInput]) {
[_captureSession addInput:frontDeviceInput];
}
}
}
// 4. 创建外外部提供数据源的输出类对象,队列管理 (数据绘制在GLKView展示)
const char *queueName = [@"sample buffer delegate" UTF8String];
_sampleBufferQueue = dispatch_queue_create(queueName , DISPATCH_QUEUE_SERIAL);
AVCaptureVideoDataOutput *videoDateOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoDateOutput setSampleBufferDelegate:self queue:_sampleBufferQueue];
if ([_captureSession canAddOutput:videoDateOutput]) {
[_captureSession addOutput:videoDateOutput];
}
// 5. 捕捉静态图片
_stillCameraOutPut = [[AVCaptureStillImageOutput alloc] init];
if ([_captureSession canAddOutput:_stillCameraOutPut]) {
[_captureSession addOutput:_stillCameraOutPut];
}
_captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
}
- (IBAction)takePhoto:(id)sender {
// 拍摄单张
AVCaptureConnection *captureSession = [_stillCameraOutPut connectionWithMediaType:AVMediaTypeVideo];
[_stillCameraOutPut captureStillImageAsynchronouslyFromConnection:captureSession completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
NSDictionary *meteData = CFBridgingRelease(CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate));
// 接下来,就是保存图片或者使用图片
}
}];
// 连拍
AVCaptureManualExposureBracketedStillImageSettings *setting1 = [AVCaptureManualExposureBracketedStillImageSettings manualExposureSettingsWithExposureDuration:CMTimeMake(1, 2) ISO:200];
AVCaptureManualExposureBracketedStillImageSettings *setting2 = [AVCaptureManualExposureBracketedStillImageSettings manualExposureSettingsWithExposureDuration:CMTimeMake(1, 4) ISO:200];
AVCaptureManualExposureBracketedStillImageSettings *setting3 = [AVCaptureManualExposureBracketedStillImageSettings manualExposureSettingsWithExposureDuration:CMTimeMake(1, 8) ISO:200];
[_stillCameraOutPut captureStillImageBracketAsynchronouslyFromConnection:captureSession withSettingsArray:@[setting1,setting2,setting3] completionHandler:^(CMSampleBufferRef sampleBuffer, AVCaptureBracketedStillImageSettings *stillImageSettings, NSError *error) {
// 回调次数与SettingsArray的设置项个数相等。
}];
}
### 视频
与图片捕捉流程差异:多一个音频输入和输出处理。AVCaptureSession 的输入源(一个或多个AVCaptureDevice,视频、音频)
1. ***AVCaptureMovieFileOutput*** 将视频和音频写入 QuickTime 文件,这只需很少的配置。
AVCaptureMovieFileOutput *movieFileOutput = [AVCaptureMovieFileOutput new];
if([captureSession canAddOutput:movieFileOutput]){
[captureSession addOutput:movieFileOutput];
}
// 开始录制
NSURL *outputURL = …
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
![image](Video_AVFoundation.png)
2. ***AVCaptureDataOutput 和 AVAssetWriter***
AVCaptureVideoDataOutput和AVCaptureAudioDataOutput会各自捕获视频和音频的样本缓存,发送到它们的代理。代理对采样缓冲进行处理 (滤镜),然后使用 AVAssetWriter 对象可以将样本缓存写入文件
![image](Video_AVFoundation2.png)
NSURL *url = …;
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];
AVAssetWriterInput *videoInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:nil];
videoInput.expectsMediaDataInRealTime = YES;
AVAssetWriterInput *audioInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:nil];
audioInput.expectsMediaDataInRealTime = YES;
if ([assetWriter canAddInput:videoInput]) {
[assetWriter addInput:videoInput];
}
if ([assetWriter canAddInput:audioInput]) {
[assetWriter addInput:audioInput];
}
### 配置
1. 配置AVCaptureSession(sessionPreset)控制输出分辨率
NSString *const AVCaptureSessionPresetPhoto; //高像素图片输出
NSString *const AVCaptureSessionPresetHigh;
NSString *const AVCaptureSessionPresetMedium;
NSString *const AVCaptureSessionPresetLow;
NSString *const AVCaptureSessionPreset352x288;
NSString *const AVCaptureSessionPreset640x480;
NSString *const AVCaptureSessionPreset1280x720;
NSString *const AVCaptureSessionPreset1920x1080;
NSString *const AVCaptureSessionPresetiFrame960x540;
NSString *const AVCaptureSessionPresetiFrame1280x720;
NSString *const AVCaptureSessionPresetInputPriority; //通过已连接的捕获设备的 activeFormat 来反过来控制 capture session 的输出质量等级。
2. 配置相机 AVCaptureDevice
iPhone 和 iPad 中内置相机的对焦,曝光(时长、感光度、镜头光圈(ios 8)调节)。
相机辅助配件:闪光灯、手电筒
##### 1) 对焦
1. 对焦是通过移动镜片改变其到传感器之间的距离实现的。
2. 自动对焦是通过相位检测和反差检测实现的.(反差检测只适用于低分辨率和高 FPS 视频捕捉 (慢镜头))
focusMode:对焦模式,AVCaptureFocusMode 是个枚举,描述了可用的对焦模式:
AVCaptureFocusModeLocked 指镜片处于固定位置
AVCaptureFocusModeAutoFocus 指一开始相机会先自动对焦一次,然后便处于 Locked 模式。
AVCaptureFocusModeContinuousAutoFocus 指当场景改变,相机会自动重新对焦到画面的中心点。
focusPointOfInterest:对焦,范围 {0,0} {1,1}
smoothAutoFocusEnabled:快速对焦 (IOS 7.0)
lensPosition:焦距(IOS 8.0)
##### 2 曝光
影响曝光三个要素:快门速度,光圈和感光度(ISO). iPhone 和iPad光圈
1. 快门速度:图像传感器捕捉光线的一段时间。
2. 光圈:用来衡量到达图像感应器的光所通过的通孔的大小。 f/2.2 镜头焦距与光圈 (也就是通孔) 的有效直径的比例.
3. 感光度(ISO):它被用来衡量图像传感器对光的灵敏程度,以及因此带来的曝光噪音。(也称为胶卷速度)
exposureMode:曝光模式,AVCaptureExposureMode,描述了可用的曝光模式:
AVCaptureExposureModeLocked 处于固定位置
AVCaptureExposureModeAutoExpose 指一开始相机会先自动对焦一次,然后便处于 Locked 模式。
AVCaptureExposureModeContinuousAutoExposure 指当场景改变,相机会自动重新调整曝光
AVCaptureExposureModeCustom (IOS 8.0) 用于自定义模式,根据用户设置的ISO、曝光时间调整曝光。
exposurePointOfInterest:曝光点
exposureDuration:曝光时长
exposureTargetOffset:当前场景的曝光档位与目标档位的偏差(接近0:标示曝光正确)
ISO:感光度
#### 3 白平衡
whiteBalanceMode:白平衡模式,AVCaptureWhiteBalanceMode描述了可用的白平衡模式。
AVCaptureWhiteBalanceModeLocked AVCaptureWhiteBalanceModeAutoWhiteBalance
AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance
deviceWhiteBalanceGains:白平衡值,AVCaptureWhiteBalanceGains基于RGB的结构体。
AVCaptureWhiteBalanceChromaticityValues:色温和色彩值。
#### 4 闪光灯
flashMode:闪光灯模式,AVCaptureFlashMode描述了可用的闪光灯模式
AVCaptureFlashModeOff 关闭
AVCaptureFlashModeOn 开启
AVCaptureFlashModeAuto 自动,根据周围的光线环境选择开和关
#### 5 手电筒
torchMode:手电筒模式, AVCaptureTorchMode描述了可用的手电筒模式
AVCaptureTorchModeOff 关闭
AVCaptureTorchModeOn 开启
AVCaptureTorchModeAuto 自动,根据周围的光线环境选择开和关
torchLevel:手电筒亮度0.0 (off) -> 1.0 (full)
#### 6 formats:设备配置参数。 AVCaptureDeviceFormat描述了一些设备使用的参数,比如静态图片分辨率,视频预览分辨率,自动对焦类型,感光度和曝光时间限制等
以上设置这些之前都需要先调用lockForConfiguration,获取操作权限;结束后 unlockForConfiguration释放权限。
### iPhone6后置摄像头的一些基于设备格式的约束参数配置的例子
格式 分辨率 FPS HRSI FOV VIS 最大放大比例 Upscales AF ISO SS HDR
420v 1280x720 5 - 240 1280x720 54.626 YES 49.12 1.09 1 29.0 - 928 0.000003-0.200000 NO
420f 1280x720 5 - 240 1280x720 54.626 YES 49.12 1.09 1 29.0 - 928 0.000003-0.200000 NO
420v 1920x1080 2 - 30 3264x1836 58.040 YES 95.62 1.55 2 29.0 - 464 0.000013-0.500000 YES
420f 1920x1080 2 - 30 3264x1836 58.040 YES 95.62 1.55 2 29.0 - 464 0.000013-0.500000 YES
420v 1920x1080 2 - 60 3264x1836 58.040 YES 95.62 1.55 2 29.0 - 464 0.000008-0.500000 YES
420f 1920x1080 2 - 60 3264x1836 58.040 YES 95.62 1.55 2 29.0 - 464 0.000008-0.500000 YES
---------------------------------------------------
格式 = 像素格式
FPS = 支持帧数范围
HRSI = 高像素静态图片尺寸
FOV = 视角
VIS = 该格式支持视频防抖
Upscales = 加入数字 upscaling 时的放大比例
AF = 自动对焦系统(1 是反差对焦,2 是相位对焦)
ISO = 支持感光度范围
SS = 支持曝光时间范围
HDR = 支持高动态范围图像
iOS 相机开发总结相关推荐
- iOS 11开发教程(一)
iOS 11开发概述 iOS 11是目前苹果公司用于苹果手机和苹果平板电脑的最新的操作系统.该操作系统的测试版于2017年6月6号(北京时间)被发布.本章将主要讲解iOS 11的新特性.以及使用Xco ...
- 收集各种 iOS App 开发可以用到的代码示例
code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 ...
- 视频教程-使用Objective-C进行iOS移动开发-iOS
使用Objective-C进行iOS移动开发 15年以上IT行业工作经验.8年以上IT行业教学经验.丰富的项目经验和授课经验,授课形式不拘一格.熟悉iOS开发,网页开发.Java开发.平面设计等技术, ...
- iOS应用开发之权限说明
在iOS应用开发的过程中,需要配置info.plist文件给应用添加权限,该文件是键值对的形式.下面对info.plist文件中的部分权限进行说明,详细内容请以苹果开发者官网为准:https://de ...
- IOS入门开发大概课程内容记录
达内课程体系 第一阶段.Web开发基础(Linux操作系统.Nigix.XHTML技术核心.CSS技术核心):课程持续3周共96课时. 模块 课程目标 内容 项目 1 Mac Fundamental ...
- 《iOS移动开发从入门到精通》图书连载一:如果你也想开发一款自己的APP,可以看一下这篇文
前言:互联网+时代给自己多一个选择的机会,尝试开发一款属于自己的APP,绝对是件激动人心的事情!<iOS移动开发从入门到精通>已经上市并和大家见面.从今天起,我会将把图书的部分内容以连载的 ...
- 视频教程-iOS移动开发从入门到精通(Xcode11 Swift5)-iOS
iOS移动开发从入门到精通(Xcode11 & Swift5) 15年以上IT行业工作经验.8年以上IT行业教学经验.丰富的项目经验和授课经验,授课形式不拘一格.熟悉iOS开发,网页开发.Ja ...
- Azure Kinect 3深度相机开发--人体姿态估计并输出各个关节点坐标
Azure Kinect 3深度相机开发--人体姿态估计并输出各个关节点坐标 前言 一.VS配置Azure Kinect 3开发环境(C++) 1.下载并安装Azure Kinect3 SDK 2.配 ...
- iOS蓝牙开发---CoreBluetooth[BLE 4.0] 初级篇[内附Demo地址]
一.蓝牙基础知识 (一)常见简称 1.MFI make for ipad ,iphone, itouch 专们为苹果设备制作的设备,开发使用ExternalAccessory 框架(认证流程貌似挺复 ...
- ios开发趋势_2020年将成为iOS应用开发的主要趋势
ios开发趋势 Technology has always brought something new with time. And with these ever-changing technolo ...
最新文章
- 计算机计算能力,计算机/电脑为什么拥有计算能力
- oracle卸载注意啥,关于oracle的安装,卸载以及其他注意事项
- 使用docker中的apline部署自己的golang的后端代码(添加制作静态服务器的注意点)...
- Angular jasmine单元测试框架里spyOn的创建原理
- 在.NET中使用DiagnosticSource
- nginx配合python_人生苦短我用python[0x02] nginx与python结合
- 信息学奥赛一本通C++语言——1040:输出绝对值
- python实现mysql的读写分离及负载均衡
- c语言从入门到秃头表情包,c语言从入门到放弃表情包 - c语言从入门到放弃微信表情包 - c语言从入门到放弃QQ表情包 - 发表情 fabiaoqing.com...
- frp内网穿透并实现开机自启动
- 运维团队(OPS)与技术团队有效沟通配合探讨
- 5.4.4. Primary Keys
- jenkins svn publisher插件使用手册
- 戴隐形眼镜好吗 隐形眼镜能经常戴吗
- 用ffmpeg给音频加封面(亲试成功)
- 做到这些让你逃离奴隶的束缚
- 以女大学生相亲为例,给你讲明白数据挖掘算法
- 微信JSSDK 签名错误invalid signature排查
- 网易云音乐编码_音乐编码依据
- 查看Office是否永久激活以及剩余可用时间
热门文章
- Pixhawk学习9——固定翼位置控制(L1控制+TECS总能量控制)
- Ubuntu问题:E45: ‘readonly‘ option is set (add ! to override)错误解决
- 从零到一搭建一个属于自己的博客系统(弍)
- laravel-excel 中设置列宽,单元格内容垂直和水平都居中
- N-Queen(java实现)
- html表格的常用样式
- C#笔记——自动关机or定时关机小程序
- 高等数学笔记:两个重要极限
- Linux学习之安装配置VM虚拟机
- wamp mysql 密码_WAMP中的mysql设置密码