一垫释、掃描二維碼需要4個對象
// 1.輸入設備
@property (nonatomic, strong) AVCaptureDeviceInput *deviceInput;
// 2.輸出設備 metadata 元數(shù)據(jù)
@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.輸出設備 -->解析數(shù)據(jù)
self.metadataOutput = [[AVCaptureMetadataOutput alloc] init];
// 設置輸出設備代理 在掃描到信息時可執(zhí)行回調(diào)
[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];
}
// 設置需要解析的數(shù)據(jù)類型
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];
三、掃描到信息時的回調(diào),如果不主動取消,會一直調(diào)用.
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//關(guān)閉會話
[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 進行數(shù)據(jù)逆?zhèn)? 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
注意:
如出現(xiàn)以下提示
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