前言:

最近项目有个需求是对试图对手机密码进行强破解的人进行拍照(通过摄像头截图),因为之前没做过,所以一堆坑。现在就把我的经验都分享出来,希望后来人不用再踏上坑途中。

直接上代码:

  // 创建会话

  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之如何从摄像头获取图像的更多相关文章

  1. 如何使用 OpenCV 打开摄像头获取图像数据?

    OpenCV 如何打开摄像头获取图像数据? 代码运行环境:Qt 5.9.1 msvc2015 32bit OpenCV 3.3.0 #include "include/opencv2/ope ...

  2. ROS 教程之 vision : 用各种摄像头获取图像

    可能有很多人想在ROS下学习视觉,先用摄像头获取图像,再用opencv做相应算法处理,可是ROS下图像的采集可不像平常的read一下那么简单,需要借助外部package的使用.而摄像头即可以用笔记本自 ...

  3. android摄像头获取图像——第三弹

    相机获取图像的格式问题 android中承认的格式的参考网址为 :http://developer.android.com/reference/android/graphics/ImageFormat ...

  4. ROS 使用自带和usb摄像头获取图像

    笔记本自带的摄像头的设备号一般为/dev/video0 第一步:安装Webcam 驱动 $ sudo apt-get install git-core $ cd ~/catkin_ws/src $ g ...

  5. Halcon学习之二:摄像头获取图像和相关参数

    1.close_all_framegrabbers ( : : : ) 关闭所有图像采集设备. 2.close_framegrabber ( : : AcqHandle : ) 关闭Handle为Ac ...

  6. android摄像头获取图像——第二弹

    使用android内的Camera对象 (1)Camera是控制着摄像头的api,拥有一系列控制摄像头的上层方法:camera类能够调用底层的摄像头接口,完成启动摄像头.预 览摄像头图像.拍照等功能: ...

  7. android摄像头获取图像——第一弹

    http://www.cnblogs.com/mengyan/archive/2012/09/01/2666636.html 安卓读取视频的几种方式: 详细讲述请参考网址:http://www.cnb ...

  8. GStreamer 从摄像头获取图像 转264

    1.这里有个简单的例子,可以看看GStreamer如何编程的. 2.GStreamer  GstAppSink的官方Document,翻译了一下它的描述部分,点击这里. 3.GStreamer  Gs ...

  9. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...

随机推荐

  1. FLV格式详解

    Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...

  2. CodeForces758D

    D. Ability To Convert time limit per test:1 second memory limit per test:256 megabytes input:standar ...

  3. .NET Core 最小化发布

    .NET Core 应用最小化独立部署发布,.NET Core 默认应用独立发布,大概占用50m左右的空间,不同的系统大小有所区别. .NET Core 的发布之前我也有所介绍,.NET Core 跨 ...

  4. loadrunner解决浏览器死机问题

    初次接触loadrunner时,遇到很多问题.浏览器崩溃以及录不到脚本就折磨了一周时间.最后终于解决 一.浏览器崩溃问题 1.退出安全卫士和防火墙 2.去掉IE第三方扩展.工具-Internet选项- ...

  5. Exiting the Matrix: Introducing Metasploit's Hardware Bridge

    Metasploit is an amazing tool. You can use it to maneuver through vast networks, pivoting through se ...

  6. 关于j2ee工程发布到was上后,部分更新,例修改web.xml配置文件不起作用的原因解析【转】

    在WAS中,应用的配置是从config/cells....目录下读取:而资源从/installedApps目录下读取 故当配置文件(例web.xml)发生改变时,只更新应用程序资源文件/install ...

  7. ArcGIS快速制图插件介绍

    ArcGIS快速制图插件介绍 By 李远祥 作品背景 <快速制图插件增强版>在原有的<快速制图插件>基础上,加入植被乱序填充.生成立体楼快.等高线增强显示.一键导出地图和数据. ...

  8. The type or namespace name '****' could not be found

    偶尔会在编译时出现“The type or namespace name '****' could not be found (are you missing a using directive or ...

  9. CAN信号值解析

    本文提供一种可以解析CAN信号各信号值的一种方法并进行说明. 一般情况下,高端一点的设备会计算每一个信号的值,但是接受到CAN信号的报文实际上有各种情况,如何通过设定的起始位和数据长度来获取某一信号的 ...

  10. 关于最优化读写,测试各个加锁:Lock、安全字典、信号量、ReaderWriterLock、ReaderWriterLockSlim

    大家对于自身项目中都有本地缓存的逻辑,但这块的读写何种机制下哪个快,哪个慢,需要测试对比,以下测试报告,期望给大家一个借鉴,如果有更好的测试结果也欢迎大家一起讨论. 以下测试是开启10个并发任务对同一 ...