一、扫描二维码需要4个对象
// 1.输入设备
@property (nonatomic, strong) AVCaptureDeviceInput *deviceInput;
// 2.输出设备 metadata 元数据
@property (nonatomic, strong) AVCaptureMetadataOutput *metadataOutput;
// 3.会话 连接输入和输出设备
@property (nonatomic, strong) AVCaptureSession *session;
// 4.预览界面 预览摄像头采集到的信息 -->特殊的图层
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
二、实例化 并配置 各对象
// 1.输入设备 -->键盘 摄像头 麦克风
// 默认是后置摄像头
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];
// 2.输出设备 -->解析数据
self.metadataOutput = [[AVCaptureMetadataOutput alloc] init];
// 设置输出设备代理 在扫描到信息时可执行回调
[self.metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 3.会话 -->连接输入和输出设备
self.session = [[AVCaptureSession alloc] init];
if ([self.session canAddInput:self.deviceInput]) {
[self.session addInput:self.deviceInput];
}
if ([self.session canAddOutput:self.metadataOutput]) {
[self.session addOutput:self.metadataOutput];
}
// 设置需要解析的数据类型
self.metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
// 4.预览界面 -->预览摄像头采集到的信息 -->特殊的图层
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
self.previewLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.previewLayer];
// 5.开启会话
[self.session startRunning];
三、扫描到信息时的回调,如果不主动取消,会一直调用.
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//关闭会话
[self.session stopRunning];
[self.previewLayer removeFromSuperlayer];
[self dismissViewControllerAnimated:YES completion:nil];
//使用Safari 控制器 加载页面
if ([[metadataObjects.firstObject stringValue] hasPrefix:@"http"]||[[metadataObjects.firstObject stringValue] hasPrefix:@"https"]) {
SFSafariViewController *safarVC = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:[metadataObjects.firstObject stringValue]]];
[self presentViewController:safarVC animated:YES completion:nil];
}
//使用 block 进行数据逆传
self.complete([metadataObjects.firstObject stringValue]);
}
四、可以自定义一个 view 完成 UI 页面设置
有两个地方需要注意:
需要改变 view 的 layer -->AVCaptureVideoPreviewLayer
需要将会话 session 传给 view
#import "NEQRPreview.h"
@interface NEQRPreview ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *lineImageView;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation NEQRPreview
//给 session 赋值
- (void)setSession:(AVCaptureSession *)session{
_session = session;
AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *) self.layer;
layer.session = session;
}
//改变 View layer
+(Class)layerClass{
return [AVCaptureVideoPreviewLayer class];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initUiConfig];
}
return self;
}
//在一个视图中设置二维码UI的垃圾代码
- (void)initUiConfig
{
//设置背景图片
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
//设置位置到界面的中间
_imageView.frame = CGRectMake(self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);
NSLog(@"%@",NSStringFromCGRect( _imageView.frame));
//添加到视图上
[self addSubview:_imageView];
//初始化二维码的扫描线的位置
_lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
_lineImageView.image = [UIImage imageNamed:@"line.png"];
[_imageView addSubview:_lineImageView];
//开启定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}
- (void)animation
{
[UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_lineImageView.frame = CGRectMake(30, 260, 220, 2);
} completion:^(BOOL finished) {
_lineImageView.frame = CGRectMake(30, 10, 220, 2);
}];
}
@end
注意:
如出现以下提示
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.
解决方法 :
在 Info.plist中加入 NSCameraUsageDescription 字段即可.
DEMO:GitHub
网友评论