语音聊天程序开发时,二维码扫描功能该如何实现

语音聊天作为娱乐社交领域深受大家喜爱的程序,开发者在进行开发工作时,经常会涉及到关于二维码扫描的功能,用户可以通过扫描二维码添加好友进行聊天和沟通,所以本文来简单介绍下在语音聊天程序开发时,该功能的大体实现流程是怎样的。
1.进入二维码扫描界面后初始化

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.    // 设置扫描范围(每一个取值0~1,以屏幕右上角为坐标原点)
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];

2.添加扫描涂层

//懵层
- (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;
}

3.扫描结束、根据扫描结果做相应操作

- (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(@"暂未识别出扫描的二维码");
    }
}

以上就是语音聊天程序开发时会涉及到的二维码扫描功能的大体实现流程,该功能的实现对于这类娱乐社交应用场景来说还是比较重要的。了解更多请继续关注,后期会不定时更新相关文章内容。
声明:本文由作者原创,转载请注明出处及原文链接。

发布了119 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/yun_bao_2144899870/article/details/102744237