二维码功能方便快捷,深受用户喜爱,本文为大家简单介绍,一对一直播系统开发想要实现在APP内实现扫描二维码功能,需要以下几步。

一、首先是二维码的获取和分析,需要一对一直播系统开发源码获取手机摄像头使用权限,设置扫描范围,进入二维码界面后,会对界面进行初始化。

  1. // 1、获取摄像设备
  2. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  3. // 2、创建摄像设备输入流
  4. AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
  5. // 3、创建元数据输出流
  6. AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
  7. [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  8. [metadataOutput setRectOfInterest:CGRectMake((self.view.frame.size.height - 220)*0.5/UIScreen.mainScreen.bounds.size.height,
  9. (self.view.frame.size.width - 220)*0.5/UIScreen.mainScreen.bounds.size.width,
  10. 220/UIScreen.mainScreen.bounds.size.height,
  11. 220/UIScreen.mainScreen.bounds.size.width)];
  12. // 设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)
  13. // 注:微信二维码的扫描范围是整个屏幕,这里并没有做处理(可不用设置);
  14. // 如需限制扫描框范围,打开下一句注释代码并进行相应调整
  15. // metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);
  16. // 4、创建会话对象
  17. _session = [[AVCaptureSession alloc] init];
  18. // 并设置会话采集率
  19. _session.sessionPreset = AVCaptureSessionPreset1920x1080;
  20. // 5、添加元数据输出流到会话对象
  21. [_session addOutput:metadataOutput];
  22. // 创建摄像数据输出流并将其添加到会话对象上, --> 用于识别光线强弱
  23. self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
  24. [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  25. [_session addOutput:_videoDataOutput];
  26. // 6、添加摄像设备输入流到会话对象
  27. [_session addInput:deviceInput];
  28. // 7、设置数据输出类型(如下设置为条形码和二维码兼容),需要将数据输出添加到会话后,才能指定元数据类型,否则会报错
  29. metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
  30. // 8、实例化预览图层, 用于显示会话对象
  31. _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
  32. // 保持纵横比;填充层边界
  33. _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  34. CGFloat x = 0;
  35. CGFloat y = 0;
  36. CGFloat w = [UIScreen mainScreen].bounds.size.width;
  37. CGFloat h = [UIScreen mainScreen].bounds.size.height;
  38. _videoPreviewLayer.frame = CGRectMake(x, y, w, h);
  39. [self.view.layer insertSublayer:_videoPreviewLayer atIndex:0];
  40. // 9、启动会话
  41. [_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(@"暂未识别出扫描的二维码");
    }
    }

以上就是一对一直播源码开发的扫描二维码功能的大体流程实现,该功能对于提高用户感受和方便用户使用都有帮助,在万物皆可扫一扫的时代背景下,开发这个功能能够加强一对一直播源码开发增强社交性、互动性,满足人们的社交需求。