AVFoundation之如何从摄像头获取图像
前言:
最近项目有个需求是对试图对手机密码进行强破解的人进行拍照(通过摄像头截图),因为之前没做过,所以一堆坑。现在就把我的经验都分享出来,希望后来人不用再踏上坑途中。
直接上代码:
// 创建会话
self.session = [[AVCaptureSession alloc] init];
// 获取摄像头的权限信息,判断是否有开启权限
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusAuthorized)
{
self.setupResult = AVCamSetupResultSuccess;
}
if ( self.setupResult != AVCamSetupResultSuccess )
return;
self.backgroundRecordingID = UIBackgroundTaskInvalid;
NSError *error = nil;
// 创建输入设备
AVCaptureDevice *videoDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionFront];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
// beginConfiguration这个很重要,在addInput或者addOutput的时候一定要调用这个,add之后再调用commitConfiguration
[self.session beginConfiguration];
if ([self.session canAddInput:videoDeviceInput])
{
[self.session addInput:videoDeviceInput];
self.videoDeviceInput = videoDeviceInput;
}
// 为会话加入output设备
dispatch_queue_t videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
videoDataOutput.videoSettings =[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]forKey: (id)kCVPixelBufferPixelFormatTypeKey];
// 设置self的AVCaptureVideoDataOutputSampleBufferDelegate
[videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];
if ([self.session canAddOutput:videoDataOutput])
{
[self.session addOutput:videoDataOutput];
}
[self.session commitConfiguration];
// 获取前置摄像头
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType];
AVCaptureDevice *captureDevice = devices.firstObject;
for ( AVCaptureDevice *device in devices ) {
if ( device.position == position ) {
captureDevice = device;
break;
}
}
return captureDevice;
}
// 实现代理方法并获取图片
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
if (takephoto.isShutScreen)
{
NSLog(@"didOutputSampleBuffer");
UIImage *image = [self getImageBySampleBufferref:sampleBuffer];
}
}
// 将获取的数据转换成图片,网上很多转换方式转出来的图片都是错误的,最后找到这个方法
- (UIImage *)getImageBySampleBufferref:(CMSampleBufferRef)sampleBuffer
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
/*Lock the image buffer*/
CVPixelBufferLockBaseAddress(imageBuffer,0);
/*Get information about the image*/
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
/*We unlock the image buffer*/
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
/*Create a CGImageRef from the CVImageBufferRef*/
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
/*We release some components*/
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
NSLog(@"%@", image);
/*We relase the CGImageRef*/
CGImageRelease(newImage);
return image;
}
只要照着我的思路走,就可以达到摄像机截屏的效果,希望能帮到需要的人,如有其他意见,请留言。
AVFoundation之如何从摄像头获取图像的更多相关文章
- 如何使用 OpenCV 打开摄像头获取图像数据?
OpenCV 如何打开摄像头获取图像数据? 代码运行环境:Qt 5.9.1 msvc2015 32bit OpenCV 3.3.0 #include "include/opencv2/ope ...
- ROS 教程之 vision : 用各种摄像头获取图像
可能有很多人想在ROS下学习视觉,先用摄像头获取图像,再用opencv做相应算法处理,可是ROS下图像的采集可不像平常的read一下那么简单,需要借助外部package的使用.而摄像头即可以用笔记本自 ...
- android摄像头获取图像——第三弹
相机获取图像的格式问题 android中承认的格式的参考网址为 :http://developer.android.com/reference/android/graphics/ImageFormat ...
- ROS 使用自带和usb摄像头获取图像
笔记本自带的摄像头的设备号一般为/dev/video0 第一步:安装Webcam 驱动 $ sudo apt-get install git-core $ cd ~/catkin_ws/src $ g ...
- Halcon学习之二:摄像头获取图像和相关参数
1.close_all_framegrabbers ( : : : ) 关闭所有图像采集设备. 2.close_framegrabber ( : : AcqHandle : ) 关闭Handle为Ac ...
- android摄像头获取图像——第二弹
使用android内的Camera对象 (1)Camera是控制着摄像头的api,拥有一系列控制摄像头的上层方法:camera类能够调用底层的摄像头接口,完成启动摄像头.预 览摄像头图像.拍照等功能: ...
- android摄像头获取图像——第一弹
http://www.cnblogs.com/mengyan/archive/2012/09/01/2666636.html 安卓读取视频的几种方式: 详细讲述请参考网址:http://www.cnb ...
- GStreamer 从摄像头获取图像 转264
1.这里有个简单的例子,可以看看GStreamer如何编程的. 2.GStreamer GstAppSink的官方Document,翻译了一下它的描述部分,点击这里. 3.GStreamer Gs ...
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...
随机推荐
- Android项目中打jar包 和 使用
第一步,把普通的android project设置成库项目 库项目也是一个标准的android项目,因此你先创建一个普通的android项目. 这个项目可以起任何的名称,任何的报名,设置其他需要设置的 ...
- JAVA轻量级文件监控
原文地址:http://blog.csdn.net/three_man/article/details/31012903?utm_source=tuicool 介绍 本文主要介绍一种轻量级的文件监控方 ...
- Mac系统安装Aircrack-ng破解附近wifi密码(1)
第一步, 安装macport, 安装Xcode 安装macport macport 是一个工具 管理软件包的一个工具, 我们也可以通过别的方式安装Aircrack-ng, 但是通过macport安装A ...
- asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- 使用C#读写ini配置文件
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...
- jquery mobile多页面跳转等,data-ajax="false" 问题,
当我们的网站引用了jquery mobile的js后,点击页面的链接,你会发现页面无法跳转,因为jquery mobile默认是采用ajax方式来加载网站的,如果你需要跳到另一个页面,需要在a标签加上 ...
- app与后台交互之间的几种安全认证机制
1.HTTP简单基本认证方式 这个是早期交互用得比较多的一种方式,主要是使用用户名和密码来交互,由于在每次的交互中,用户名和密码都会暴露给第三方,那么这么做是不可取的,风险十分大,所以这种认证方式并没 ...
- Unity编程标准导引-1.2官方资源介绍
1.2.官方资源介绍 Unity官方提供了丰富的学习和参考资源,有以下类别: Unity手册以及API文档 Unity的官方教程 AssetStore 1.2.1 Unity手册以及API文档 前述文 ...
- oracle 11G RAC会话故障转移测试
目前接手的几个项目中,默认使用的oracle RAC数据库服务,均不能实现自动的会话转移,尤其是对于应用的长连接,一旦发生数据库故障,需要重启应用.实际11G具备会话迁移机制,为此做了如下配置测试,供 ...
- 关于post与get请求参数存在特殊字符问题
遇到项目中存在文本编辑框输入特殊字符 比如:# ? & 空格 , 导致后台接受不到参数问题,对可能存在特殊字符的参数进行encodeURIComponent; C#后台接受参数不需要解码 也可 ...