视频捕捉、获取静态图片(自定义相机)
其实这相当于是一个自定义相机的功能了
github下载链接:https://github.com/SSYSSK/camera2
1、自定义相机
2、视频的捕获预览
3、视频的录制
4、拍照
5、解决了拍照照片翻转90度的问题
6、解决了前置摄像头照片颠倒的问题
关键点:
1、自从iOS10之后,获取图片的输出由AVCaptureStillImageOutput变成了AVCapturePhotoOutput,所以需要做两套适配,输出配置代码:
//6、静态图片输出if (@available(iOS 10.0, *)) {self.imageOutput = [[AVCapturePhotoOutput alloc]init];//输出连接 判断是否可用,可用则添加到输出连接中去if ([self.captureSession canAddOutput:self.imageOutput]){[self.captureSession addOutput:self.imageOutput];}}else {//AVCaptureStillImageOutput 实例 从摄像头捕捉静态图片self.stillImageOutput = [[AVCaptureStillImageOutput alloc]init];//配置字典:希望捕捉到JPEG格式的图片self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecTypeJPEG};if ([self.captureSession canAddOutput:self.stillImageOutput]){[self.captureSession addOutput:self.stillImageOutput];}}
2、拍照功能,要区分iOS10和iOS10之前:
[output capturePhotoWithSettings:settings delegate:self];
// 从流中获取照片
-(void)getPhoto {// 拍照if (@available(iOS 10.0, *)) {AVCapturePhotoOutput * output = (AVCapturePhotoOutput *)self.imageOutput;AVCapturePhotoSettings * settings = [AVCapturePhotoSettings photoSettings];// 这一句代码才是执行拍照[output capturePhotoWithSettings:settings delegate:self];}else{AVCaptureStillImageOutput * stillImageOutput = (AVCaptureStillImageOutput *)self.stillImageOutput;//从 AVCaptureStillImageOutput 中取得 AVCaptureConnectionAVCaptureConnection *connection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];[stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef _Nullable imageDataSampleBuffer, NSError * _Nullable error) {if (imageDataSampleBuffer != nil) {NSData * data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];UIImage *image = [[UIImage alloc]initWithData:data];//重点:捕捉图片成功后,将图片传递出去UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}}];}}
3、拍照完成的代理回调(iOS10之后才会走这里),这里要注意一个照片翻转的问题:
//前置摄像头拍照会旋转180解决办法
if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {
UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}else {
UIImageOrientation imgOrientation = UIImageOrientationRight;
image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
}
#pragma mark AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {if (error) {NSLog(@"获取图片错误 --- %@",error.localizedDescription);}if (photo) {if (@available(iOS 11.0, *)) {CGImageRef cgImage = [photo CGImageRepresentation];UIImage * image = [UIImage imageWithCGImage:cgImage];NSLog(@"获取图片成功 --- %@",image);//前置摄像头拍照会旋转180解决办法if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];}else {UIImageOrientation imgOrientation = UIImageOrientationRight;image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];}//重新画一张图片(将时间/个人信息/地址信息画上去)
// self.image = [self drawMarkImage:image];UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);} else {NSLog(@"不是走这个代理方法");}}
}
视频捕捉、获取静态图片(自定义相机)相关推荐
- 实现炫酷的获取本地图片和相机拍照图片-自定义组件
IOS中有封装好的选择图片后长按出现动画删除效果,效果如下 而Android找了很久都没有找到有这样效果的第三方组件,最后懒得找了还是自己实现这效果吧 选择图片后还可对图片进行剪裁 当然,代码中还有很 ...
- springboot获取静态图片路径_SpringBoot中的全局异常处理你确定你真的知道吗?
本篇要点 介绍SpringBoot默认的异常处理机制. 如何定义错误页面. 如何自定义异常数据. 如何自定义视图解析. 介绍@ControllerAdvice注解处理异常. 一.SpringBoot默 ...
- springboot获取静态图片路径_springboot为实体追加图片路径
在项目中需要同时返回半路径和全路径两种 思考解决方案,想到的相对合理的方式 1.在application.properties配置图片资源库路径 例:host.url=http://www.baidu ...
- springboot获取静态图片路径_Springboot通过图片路径形式获取图片
Springboot通过图片路径形式获取图片 一致以来都是用 http://127.0.0.1:8888/getPhoto?imgUrl=1.jpg 的形式获取数据,今天突然要 http://127. ...
- 自定义相机Camera,相机/视频实时滤镜 - android
使用相机或自定义相机,借助Android SDK Camera类的,或Camera2的类.全新的设计的Camera2是从 SDK 5.0(API Level 21)开始才被引入的,取代原来Camera ...
- iOS---系统相册视频(一)之打开照相机拍照或者录像/打开手机的相册或者视频/视频压缩/自定义相机界面/照片或视频保存到相册
参考: 打开相册和相机的ZLPhotoBrowser:https://github.com/longitachi/ZLPhotoBrowser QBImagePicker:https://github ...
- iOS开发-自定义相机(仿微信)拍照、视频录制
网上有很多自定义相机的例子,这里只是我临时写的一个小demo,仅供参考: 用到了下面几个库: #import <AVFoundation/AVFoundation.h> #import & ...
- Android自定义相机拍照、图片裁剪的实现
原文:Android自定义相机拍照.图片裁剪的实现 最近项目里面又要加一个拍照搜题的功能,也就是用户对着不会做的题目拍一张照片,将照片的文字使用ocr识别出来,再调用题库搜索接口搜索出来展示给用户,类 ...
- java对静态图片/Gif图片/视频进行水印
对静态图片/Gif图片/视频进行水印 刚开始写博客.我用在公司里写的视频/图片/gif图片水印分享出来.有任何不当之处请尽情指出.本人qq:2768861003 静态图片水印 静态图片水印,主要是通过 ...
最新文章
- 用C#对ADO.NET数据库完成简单操作
- 正交投影与最佳最小二乘解
- Java虚拟机学习集锦是我攒来的,带你碾压面试官!
- mysql -- 索引的使用
- ISO8583报文协议详解
- 后“量子霸权”时代你不可错过的几本好书
- 【GoLang】golang 最佳实践汇总
- 向量二次规划matlab,MATLAB中使用Opti Toolbox的混合整数二次规划
- java contains 通配符_java 泛型通配符 extends, super
- win10下搭建QTP测试环境
- 国密gmtls协议-双证书体系的服务端和客户端通信代码
- 利用python实现简单的爬虫,爬百度文库相关词条信息
- 《猎头局中局》——摘要
- 好物分享 | 也许是最好用的文件/目录对比软件
- 《平衡掌控者 游戏数值战斗设计》学习笔记(四)技能与装备设计
- Esp8266进阶之路14 esp8266的 FreeRtos系统学习的正确姿势,环境配置环境、烧录。(附带demo)
- 用python画枫叶代码-Python自定义函数基础
- 软件工程师薪酬最高的25家公司!!!
- CPU比GPU训练神经网络快十几倍,英特尔:别用矩阵运算了
- 之于图片主色调提取算法
热门文章
- 一文看懂数据清洗:缺失值、异常值和重复值的处理
- 【转】如何挽救一个想自杀的人
- SpringSecurity登录流程详解
- kubebuilder实践笔记(4) - 编写简单的业务逻辑
- 上位机软件定制开发,应该如何选择软件开发服务商
- mobaxterm 传文件夹_MobaXterm怎么上传文件-MobaXterm实现linux和windows之间传输文件的具体步骤...
- 人脉变现小程序裂变定制开发
- 微软应用商店的服务器,微软也玩起「快应用」,首批 PWA 应用登陆微软商店
- [计蒜客][二分]切割钢管
- Android 基于agora 视频会议开发