二维码扫描
2017-07-20 本文已影响0人
董立权
- 输入设备
- 输出设备
- 会话
- 预览图层
- 开启会话
导入框架 权限
#import <AVFoundation/AVFoundation.h>
<key>NSCameraUsageDescription</key>
<string></string>
输入设备
//输出设备
@property(strong,nonatomic)AVCaptureDeviceInput *deviceInput;
//----------------------------------------------------------------
//1.1摄像头的设备 默认后置摄像头
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//1.2创建摄像头的输入设备
self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
输出设备
//输出设备 Metadata元数据
@property(strong,nonatomic)AVCaptureMetadataOutput *output;
//----------------------------------------------------------------
//2.输出设备 ->解析数据
self.output = [[AVCaptureMetadataOutput alloc] init];
//设置代理 用来获取数据
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
//扫描到二维码数据时调用
//metadataObjects:扫描到的数据
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//扫描到数据之后 移除图层
[self.previewLayer removeFromSuperlayer];
//停止会话
[self.session stopRunning];
for (AVMetadataMachineReadableCodeObject *objc in metadataObjects) {
//二维码扫描出来的结果是字符串
NSLog(@"%@",objc.stringValue);
//创建safari控制器#import <SafariServices/SafariServices.h>
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:objc.stringValue]];
[self presentViewController:safariVC animated:YES completion:nil];
}
}
会话
//会话
@property(nonatomic,strong)AVCaptureSession *session;
//----------------------------------------------------------------
//3.会话 ->连接输入和输出设备
self.session = [[AVCaptureSession alloc] init];
//连接设备
if([self.session canAddInput:self.deviceInput]){
[self.session addInput:self.deviceInput];
}
if([self.session canAddOutput:self.output]){
[self.session addOutput:self.output];
}
//设置输出设备的解析数据的类型
//AVMetadataObjectTypeQRCode 二维码
self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
预览图层
//图层
@property(nonatomic,strong)AVCaptureVideoPreviewLayer *previewLayer;
//-------------------------------------------------------------------------
//4.预览的图层
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
//添加图层
[self.view.layer addSublayer:self.previewLayer];
//设置图层的大小
self.previewLayer.frame = self.view.bounds;
开启会话
//5.开启会话
[self.session startRunning];