二维码功能方便快捷,深受用户喜爱,本文为大家简单介绍,一对一直播系统开发想要实现在APP内实现扫描二维码功能,需要以下几步。
一、首先是二维码的获取和分析,需要一对一直播系统开发源码获取手机摄像头使用权限,设置扫描范围,进入二维码界面后,会对界面进行初始化。
- // 1、获取摄像设备
- AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
- // 2、创建摄像设备输入流
- AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
- // 3、创建元数据输出流
- AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
- [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
- [metadataOutput setRectOfInterest:CGRectMake((self.view.frame.size.height - 220)*0.5/UIScreen.mainScreen.bounds.size.height,
- (self.view.frame.size.width - 220)*0.5/UIScreen.mainScreen.bounds.size.width,
- 220/UIScreen.mainScreen.bounds.size.height,
- 220/UIScreen.mainScreen.bounds.size.width)];
- // 设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)
- // 注:微信二维码的扫描范围是整个屏幕,这里并没有做处理(可不用设置);
- // 如需限制扫描框范围,打开下一句注释代码并进行相应调整
- // metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);
- // 4、创建会话对象
- _session = [[AVCaptureSession alloc] init];
- // 并设置会话采集率
- _session.sessionPreset = AVCaptureSessionPreset1920x1080;
- // 5、添加元数据输出流到会话对象
- [_session addOutput:metadataOutput];
- // 创建摄像数据输出流并将其添加到会话对象上, --> 用于识别光线强弱
- self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
- [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
- [_session addOutput:_videoDataOutput];
- // 6、添加摄像设备输入流到会话对象
- [_session addInput:deviceInput];
- // 7、设置数据输出类型(如下设置为条形码和二维码兼容),需要将数据输出添加到会话后,才能指定元数据类型,否则会报错
- metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
- // 8、实例化预览图层, 用于显示会话对象
- _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
- // 保持纵横比;填充层边界
- _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
- CGFloat x = 0;
- CGFloat y = 0;
- CGFloat w = [UIScreen mainScreen].bounds.size.width;
- CGFloat h = [UIScreen mainScreen].bounds.size.height;
- _videoPreviewLayer.frame = CGRectMake(x, y, w, h);
- [self.view.layer insertSublayer:_videoPreviewLayer atIndex:0];
- // 9、启动会话
- [_session startRunning];
二、添加一对一直播系统开发源码扫描涂层,设置扫描蒙版,检测边框、镂空、二维码图标的四个角角落。
//懵层
-
(UIView )hudView
{
if (!_hudView) {
_hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight)];
CGFloat x = (self.view.frame.size.width - 220)
0.5;
CGFloat y = (self.view.frame.size.height - 220)0.4;
CGFloat height = 220;
//镂空
CGRect qrRect = CGRectMake(x,y,height, height);
UIBezierPath
path = [UIBezierPath bezierPathWithRoundedRect:self.view.frame cornerRadius:0];
UIBezierPath circlePath = [UIBezierPath bezierPathWithRect:qrRect];
[path appendPath:circlePath];
[path setUsesEvenOddFillRule:YES];
CAShapeLayer
fillLayer = [CAShapeLayer layer];
fillLayer.path = path.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4].CGColor;
fillLayer.opacity = 0.5;
[_hudView.layer addSublayer:fillLayer];//白色矩形 UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, height, height)]; CAShapeLayer *shapLayer = [CAShapeLayer layer]; shapLayer.backgroundColor = UIColor.clearColor.CGColor; shapLayer.path = bezierPath.CGPath; shapLayer.lineWidth = 0.5; shapLayer.strokeColor = UIColor.whiteColor.CGColor; shapLayer.fillColor = UIColor.clearColor.CGColor; [_hudView.layer addSublayer:shapLayer]; //红色四个角落 UIBezierPath *cornerBezierPath = [UIBezierPath bezierPath]; [cornerBezierPath moveToPoint:CGPointMake(x, y+30)];//左上角 [cornerBezierPath addLineToPoint:CGPointMake(x, y)]; [cornerBezierPath addLineToPoint:CGPointMake(x+30, y)]; [cornerBezierPath moveToPoint:CGPointMake(x+height-30, y)];//右上角 [cornerBezierPath addLineToPoint:CGPointMake(x+height, y)]; [cornerBezierPath addLineToPoint:CGPointMake(x+height, y+30)]; [cornerBezierPath moveToPoint:CGPointMake(x+height, y+height-30)];//左上角 [cornerBezierPath addLineToPoint:CGPointMake(x+height, y+height)]; [cornerBezierPath addLineToPoint:CGPointMake(x+height-30, y+height)]; [cornerBezierPath moveToPoint:CGPointMake(x+30, y+height)];//左上角 [cornerBezierPath addLineToPoint:CGPointMake(x, y+height)]; [cornerBezierPath addLineToPoint:CGPointMake(x, y+height-30)]; CAShapeLayer *cornerShapLayer = [CAShapeLayer layer]; cornerShapLayer.backgroundColor = UIColor.clearColor.CGColor; cornerShapLayer.path = cornerBezierPath.CGPath; cornerShapLayer.lineWidth = 3.0; cornerShapLayer.strokeColor = [UIColor redColor].CGColor; cornerShapLayer.fillColor = UIColor.clearColor.CGColor; [_hudView.layer addSublayer:cornerShapLayer];
}
return _hudView;
}
三、扫描完成,对扫描结果进行分析和处理。一般一对一直播源码的扫描结果分为两种。
1、扫描结果分析成功,跳转相关页面
2、扫描结果解析失败,显示暂未识别出扫描结果。
-
(void)captureOutput:(AVCaptureOutput )captureOutput didOutputMetadataObjects:(NSArray )metadataObjects fromConnection:(AVCaptureConnection )connection {
if (metadataObjects != nil && metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject
obj = metadataObjects[0];
NSDictionary infoDic = [self convertJsonStringToNSDictionary:[obj stringValue]];br/>NSLog(@"sweepcodeVC--------:%@",infoDic);
if ([[infoDic valueForKey:@"scope"] isEqual:@"laolaiwang"]) {
if ([minstr([[infoDic valueForKey:@"data"] valueForKey:@"type"]) isEqual:@"1"]) {
[_session stopRunning] ;
otherUserMsgVC
person = [[otherUserMsgVC alloc]init];
person.userID = minstr([[infoDic valueForKey:@"data"] valueForKey:@"uid"]);
[self.navigationController pushViewController:person animated:YES];
}else if ([minstr([[infoDic valueForKey:@"data"] valueForKey:@"type"]) isEqual:@"2"]){
[self loginManagerWithDic:infoDic];
}}
} else {br/>NSLog(@"暂未识别出扫描的二维码");
}
}
以上就是一对一直播源码开发的扫描二维码功能的大体流程实现,该功能对于提高用户感受和方便用户使用都有帮助,在万物皆可扫一扫的时代背景下,开发这个功能能够加强一对一直播源码开发增强社交性、互动性,满足人们的社交需求。
来源:oschina
链接:https://my.oschina.net/u/4343260/blog/4524424