分享

一对一直播系统开发如何在页面内实现扫描二维码功能

 昵称70678147 2020-08-21

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

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

2.    // 1、获取摄像设备

3.    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

4.    

5.    // 2、创建摄像设备输入流

6.    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

7.    

8.    // 3、创建元数据输出流

9.    AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];

10.    [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

11.    [metadataOutput setRectOfInterest:CGRectMake((self.view.frame.size.height - 220)*0.5/UIScreen.mainScreen.bounds.size.height,

12.                                          (self.view.frame.size.width - 220)*0.5/UIScreen.mainScreen.bounds.size.width,

13.                                          220/UIScreen.mainScreen.bounds.size.height,

14.                                          220/UIScreen.mainScreen.bounds.size.width)];

15.    // 设置扫描范围(每一个取值01,以屏幕右上角为坐标原点)

16.    // 注:微信二维码的扫描范围是整个屏幕,这里并没有做处理(可不用设置);

17.    // 如需限制扫描框范围,打开下一句注释代码并进行相应调整

18.    //    metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);

19.    

20.    // 4、创建会话对象

21.    _session = [[AVCaptureSession alloc] init];

22.    // 并设置会话采集率

23.    _session.sessionPreset = AVCaptureSessionPreset1920x1080;

24.    

25.    // 5、添加元数据输出流到会话对象

26.    [_session addOutput:metadataOutput];

27.    

28.    // 创建摄像数据输出流并将其添加到会话对象上,  --> 用于识别光线强弱

29.    self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];

30.    [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

31.    [_session addOutput:_videoDataOutput];

32.    

33.    // 6、添加摄像设备输入流到会话对象

34.    [_session addInput:deviceInput];

35.    

36.    // 7、设置数据输出类型(如下设置为条形码和二维码兼容),需要将数据输出添加到会话后,才能指定元数据类型,否则会报错

37.    metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code,  AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

38.    

39.    // 8、实例化预览图层, 用于显示会话对象

40.    _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

41.    // 保持纵横比;填充层边界

42.    _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

43.    CGFloat x = 0;

44.    CGFloat y = 0;

45.    CGFloat w = [UIScreen mainScreen].bounds.size.width;

46.    CGFloat h = [UIScreen mainScreen].bounds.size.height;

47.    _videoPreviewLayer.frame = CGRectMake(x, y, w, h);

48.    [self.view.layer insertSublayer:_videoPreviewLayer atIndex:0];

49.    

50.    // 9、启动会话

51.    [_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]];

        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 {

        NSLog(@"暂未识别出扫描的二维码");

    }

}

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多