一對一直播系統開發如何在頁面內實現掃描二維碼功能

二維碼功能方便快捷,深受用戶喜好,本文爲你們簡單介紹,一對一直播系統開發想要實如今APP內實現掃描二維碼功能,須要如下幾步。微信

1、首先是二維碼的獲取和分析,須要一對一直播系統開發源碼獲取手機攝像頭使用權限,設置掃描範圍,進入二維碼界面後,會對界面進行初始化。session

  1. // 一、獲取攝像設備
  2. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  3. // 二、建立攝像設備輸入流
  4. AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
  5. // 三、建立元數據輸出流
  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. // 四、建立會話對象
  17. _session = [[AVCaptureSession alloc] init];
  18. // 並設置會話採集率
  19. _session.sessionPreset = AVCaptureSessionPreset1920x1080;
  20. // 五、添加元數據輸出流到會話對象
  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. // 六、添加攝像設備輸入流到會話對象
  27. [_session addInput:deviceInput];
  28. // 七、設置數據輸出類型(以下設置爲條形碼和二維碼兼容),須要將數據輸出添加到會話後,才能指定元數據類型,不然會報錯
  29. metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
  30. // 八、實例化預覽圖層, 用於顯示會話對象
  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. // 九、啓動會話
  41. [_session startRunning];

2、添加一對一直播系統開發源碼掃描塗層,設置掃描蒙版,檢測邊框、鏤空、二維碼圖標的四個角角落。app

//懵層ide

  • (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];ui

    //白色矩形
    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;
    }spa

3、掃描完成,對掃描結果進行分析和處理。通常一對一直播源碼的掃描結果分爲兩種。
一、掃描結果分析成功,跳轉相關頁面
二、掃描結果解析失敗,顯示暫未識別出掃描結果。rest

  • (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];
    }code

    }

    } else {br/>NSLog(@"暫未識別出掃描的二維碼");
    }
    }對象

以上就是一對一直播源碼開發的掃描二維碼功能的大致流程實現,該功能對於提升用戶感覺和方便用戶使用都有幫助,在萬物皆可掃一掃的時代背景下,開發這個功能可以增強一對一直播源碼開發加強社交性、互動性,知足人們的社交需求。ci

相關文章
相關標籤/搜索