二維碼掃描主要使用蘋果自帶的AVFoundation框架
設(shè)置二維碼周邊黑框
CGFloat width = 70 * UISCALE;
CGFloat topHeight = 160 * UISCALE;
CGFloat bottomHeight = SCREEN_HEIGHT - 360 * UISCALE;
self.imgView.frame = CGRectMake(width, topHeight, SCREEN_WIDTH - width * 2, SCREEN_HEIGHT - topHeight - bottomHeight);
//最上部view
CGFloat alpha = 0.6;
CGFloat withWhite = 0.3;
UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, topHeight)];
upView.backgroundColor = [UIColor colorWithWhite:withWhite alpha:alpha];
[self.view addSubview:upView];
//左側(cè)的view
UIView * cLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, topHeight, width, 200 * UISCALE)];
cLeftView.backgroundColor = [UIColor colorWithWhite:withWhite alpha:alpha];
[self.view addSubview:cLeftView];
//右側(cè)的view
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width, topHeight, width, 200 * UISCALE)];
rightView.backgroundColor = [UIColor colorWithWhite:withWhite alpha:alpha];
[self.view addSubview:rightView];
//底部view
UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 360 * UISCALE, SCREEN_WIDTH, bottomHeight)];
downView.backgroundColor = [UIColor colorWithWhite:withWhite alpha:alpha];
[self.view addSubview:downView];
//用于說明的label
UILabel * labIntroudction= [[UILabel alloc] init];
labIntroudction.frame = CGRectMake(0, 10 * UISCALE, SCREEN_WIDTH, 30 * UISCALE);
labIntroudction.textAlignment = NSTextAlignmentCenter;
labIntroudction.textColor = [UIColor colorWithWhite:0.9 alpha:0.9];
labIntroudction.text = @"對(duì)準(zhǔn)二維碼团赁,即可自動(dòng)掃描";
labIntroudction.font = FontWithSize(15);
[downView addSubview:labIntroudction];
設(shè)置二維碼掃描layer
// 1痊班、 獲取攝像設(shè)備
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2、 創(chuàng)建輸入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
// 3兼砖、 創(chuàng)建輸出流
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
// 4、設(shè)置代理 在主線程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 設(shè)置掃描范圍(每一個(gè)取值0~1锻狗,以屏幕右上角為坐標(biāo)原點(diǎn))(微信并沒有設(shè)置祟同, 整個(gè)View都是掃描區(qū)域)
[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification
object:nil
queue:[NSOperationQueue currentQueue]
usingBlock: ^(NSNotification *_Nonnull note) {
output.rectOfInterest = [self.previewLayer metadataOutputRectOfInterestForRect:self.imgView.frame];
}];
// 5、 初始化鏈接對(duì)象(會(huì)話對(duì)象)
self.session = [[AVCaptureSession alloc] init];
// 高質(zhì)量采集率
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
// 5.1 添加會(huì)話輸入
if ([self.session canAddInput:input]) {
[self.session addInput:input];
}
// 5.2 添加會(huì)話輸出
if ([self.session canAddOutput:output]) {
[self.session addOutput:output];
//設(shè)置掃碼支持的編碼格式(如下設(shè)置條形碼和二維碼兼容)
NSMutableArray *array = [[NSMutableArray alloc] init];
if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) {
[array addObject:AVMetadataObjectTypeQRCode];
}
if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN13Code]) {
[array addObject:AVMetadataObjectTypeEAN13Code];
}
if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeEAN8Code]) {
[array addObject:AVMetadataObjectTypeEAN8Code];
}
if ([output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeCode128Code]) {
[array addObject:AVMetadataObjectTypeCode128Code];
}
output.metadataObjectTypes = array;
}
// 7厢蒜、實(shí)例化預(yù)覽圖層, 傳遞_session是為了告訴圖層將來顯示什么內(nèi)容
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.previewLayer.frame = [UIScreen mainScreen].bounds;
// 8减响、將圖層插入當(dāng)前視圖
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
// 9、啟動(dòng)會(huì)話
[self.session startRunning];
AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
// 1郭怪、設(shè)置界面顯示掃描結(jié)果
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
NSLog(@"%@", obj.stringValue);
PCBaseWebViewController *webView = [[PCBaseWebViewController alloc] init];
webView.url = [NSURL URLWithString:obj.stringValue];
[self.navigationController pushViewController:webView animated:YES];
}
// 會(huì)頻繁的掃描支示,調(diào)用代理方法
// 2、如果掃描完成鄙才,停止會(huì)話
[self.session stopRunning];
// 3颂鸿、刪除預(yù)覽圖層
[self.previewLayer removeFromSuperlayer];
}