二維碼功能方便快捷,深受用戶喜愛,本文為大家簡單介紹,一對一直播系統開發想要實現在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. // 設置掃描范圍(每一個取值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]; 二、添加一對一直播系統開發源碼掃描涂層,設置掃描蒙版,檢測邊框、鏤空、二維碼圖標的四個角角落。 //懵層 - (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(@"暫未識別出掃描的二維碼"); } } 以上就是一對一直播源碼開發的掃描二維碼功能的大體流程實現,該功能對于提高用戶感受和方便用戶使用都有幫助,在萬物皆可掃一掃的時代背景下,開發這個功能能夠加強一對一直播源碼開發增強社交性、互動性,滿足人們的社交需求。 |
|
來自: 昵稱70678147 > 《待分類》