二维码扫描主要使用苹果自带的AVFoundation框架
设置二维码周边黑框
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];
//左侧的view
UIView * cLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, topHeight, width, 200 * UISCALE)];
cLeftView.backgroundColor = [UIColor colorWithWhite:withWhite alpha:alpha];
[self.view addSubview:cLeftView];
//右侧的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 = @"对准二维码,即可自动扫描";
labIntroudction.font = FontWithSize(15);
[downView addSubview:labIntroudction];
设置二维码扫描layer
// 1、 获取摄像设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 2、 创建输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
// 3、 创建输出流
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
// 4、设置代理 在主线程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)(微信并没有设置, 整个View都是扫描区域)
[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification
object:nil
queue:[NSOperationQueue currentQueue]
usingBlock: ^(NSNotification *_Nonnull note) {
output.rectOfInterest = [self.previewLayer metadataOutputRectOfInterestForRect:self.imgView.frame];
}];
// 5、 初始化链接对象(会话对象)
self.session = [[AVCaptureSession alloc] init];
// 高质量采集率
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
// 5.1 添加会话输入
if ([self.session canAddInput:input]) {
[self.session addInput:input];
}
// 5.2 添加会话输出
if ([self.session canAddOutput:output]) {
[self.session addOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
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、实例化预览图层, 传递_session是为了告诉图层将来显示什么内容
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.previewLayer.frame = [UIScreen mainScreen].bounds;
// 8、将图层插入当前视图
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
// 9、启动会话
[self.session startRunning];
AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
// 1、设置界面显示扫描结果
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];
}
// 会频繁的扫描,调用代理方法
// 2、如果扫描完成,停止会话
[self.session stopRunning];
// 3、删除预览图层
[self.previewLayer removeFromSuperlayer];
}
网友评论