iOS 使用AVFoundation,捕捉静态图像

一、引入AVFoundation框架

添加头文件

#import <AVFoundation/AVFoundation.h>

二、定义以下对象:

AVCaptureDevice * device;  // 图像捕捉设备

AVCaptureDeviceInput * input; // 输入流

AVCaptureStillImageOutput * output; //输出流

 AVCaptureSession * session; // 输入设备和输出设备之间的数据传递

AVCaptureVideoPreviewLayer * preview; // 预览图层,显示摄像头捕捉到的画面

 UIButton * cameraButton;  // 拍照按钮

三、初始化对象

//1.实例化捕捉会话

self.session = [[AVCaptureSession alloc]init];

// 将捕捉会话的预设设置为图像

self.session.sessionPreset = AVCaptureSessionPresetPhoto;

//2.初始化捕捉设备

//self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

self.device = [self backCamera];  // 这里使用后置摄像头

//3.用captureDevice创建输入流

self.input = [[AVCaptureDeviceInput alloc]initWithDevice:self.device error:nil];

// 将输入流添加到会话中

if ([self.session canAddInput:self.input]) {

[self.session addInput:self.input];

}

//4.创建媒体数据输出流为静态图像

self.output = [[AVCaptureStillImageOutput alloc]init];

// 将输出流设置成JPEG的图片格式输出,这里输出流的设置参数AVVideoCodecJPEG参数表示以JPEG的图片格式输出图片

NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];

[self.output setOutputSettings:outputSettings];

// 5.将输出流添加到会话中

if ([self.session canAddOutput:self.output]) {

[self.session addOutput:self.output];

}

// 6. 通过会话创建预览图层

self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];

// 设置预览图层填充方式

self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

CGFloat max = MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

CGFloat min = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

// 设置预览图层的大小

self.preview.frame = CGRectMake(0, 0, min, max);

// 将预览图层放置在最上面

[self.view.layer insertSublayer:self.preview atIndex:0];



- (void)viewDidAppear:(BOOL)animated{

if (self.session) {

[self.session startRunning];

}

}


- (void)viewDidDisappear:(BOOL)animated{

if (self.session) {

[self.session stopRunning];

}

}

四、初始化设备时,可选择前置或后置摄影头;

// 根据位置获取前后摄像头设备

- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {

    AVCaptureDeviceDiscoverySession * deviceDiscoverSession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:position];

    NSArray * devices = deviceDiscoverSession.devices;

    for (AVCaptureDevice *device in devices) {

        if ([device position] == position) {

        return device;

        }

    }

return nil;

}

// 获取前置摄像头

- (AVCaptureDevice *)frontCamera {

return [self cameraWithPosition:AVCaptureDevicePositionFront];

}

// 获取后置摄像头

- (AVCaptureDevice *)backCamera {

return [self cameraWithPosition:AVCaptureDevicePositionBack];

}

// 切换摄像头

- (void)switchDevice{

NSArray *inputs = self.session.inputs;

for ( AVCaptureDeviceInput *input in inputs ) {

AVCaptureDevice *device = input.device;

if ( [device hasMediaType:AVMediaTypeVideo] ) {

AVCaptureDevicePosition position = device.position;

AVCaptureDevice *newCamera = nil;

AVCaptureDeviceInput *newInput = nil;

if (position == AVCaptureDevicePositionFront)

newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];

else

newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];

newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];

[self.session beginConfiguration];

[self.session removeInput:input];

[self.session addInput:newInput];

[self.session commitConfiguration];

break;

}

}

}

五、在拍照按钮的点击事件中获取捕捉到的图像,并可将其转成base64字符串

- (void)captureStillImage{

AVCaptureConnection * videoConnection = [self.output connectionWithMediaType:AVMediaTypeVideo];

if (!videoConnection) {

NSLog(@"take photo failed!");

return;

}

[self.output captureStillImageAsynchronouslyFromConnection:videoConnection

completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

if (imageDataSampleBuffer == NULL) {

return;

}

NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

UIImage * image = [UIImage imageWithData:imageData];

UIImage * smallImage = [self scaleImage:image toScale:0.5];

NSData * resultImageData = UIImageJPEGRepresentation(smallImage, 1.0);

NSString * imageBase64String = [resultImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

}];

}

P.S. 这里可以将获取到的图片进行缩小;

// 等比率缩放图片

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));

[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];

UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return scaledImage;

}

六、横屏时,调整预览图层frame

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

CGFloat max = MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

CGFloat min = MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {

self.preview.frame = CGRectMake(0, 0, max, min);

}

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

self.preview.frame = CGRectMake(0, 0, max, min);

}

if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {

self.preview.frame = CGRectMake(0, 0, min, max);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 由于我们公司不是专门做直播的, 所以研究直播开发完全处于兴趣爱好,可能很多地方用处理的不是很周到, 所以, 希望大...
    叫我丰叔阅读 10,258评论 28 76
  • 目录相机基本实现步骤捕捉会话——AVCaptureSession捕捉输入——AVCaptureDeviceInpu...
    cdcyd阅读 34,026评论 33 163
  • 1、为什么使用二维码? 在手机客户端应用里,对二维码的使用也比较多。常见的功能有物品信息查询,登录信息验证,扫描登...
    方圆几度阅读 509评论 0 0
  • /* 自定义相机:四个核心对象;前后摄像头的对调;拍照和保存;裁剪图片;水印图片; */ #import "Two...
    rebeccaBull阅读 461评论 0 0
  • 昨天是我的今年第二次生日,因为要润一个月,所以就有了这么一回事,早上和老爸一起去菜市场买菜,买了鱼买了牛肉,基本上...
    坚志阅读 322评论 0 0