二維碼功能方便快捷,,深受用戶喜愛,,本文為大家簡單介紹,,一對一直播系統(tǒng)開發(fā)想要實現(xiàn)在APP內(nèi)實現(xiàn)掃描二維碼功能,需要以下幾步,。 一,、首先是二維碼的獲取和分析,需要一對一直播系統(tǒng)開發(fā)源碼獲取手機攝像頭使用權(quán)限,,設(shè)置掃描范圍,,進入二維碼界面后,會對界面進行初始化,。 2. // 1,、獲取攝像設(shè)備 3. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 4. 5. // 2、創(chuàng)建攝像設(shè)備輸入流 6. AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; 7. 8. // 3,、創(chuàng)建元數(shù)據(jù)輸出流 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. // 設(shè)置掃描范圍(每一個取值0~1,,以屏幕右上角為坐標(biāo)原點) 16. // 注:微信二維碼的掃描范圍是整個屏幕,這里并沒有做處理(可不用設(shè)置); 17. // 如需限制掃描框范圍,,打開下一句注釋代碼并進行相應(yīng)調(diào)整 18. // metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6); 19. 20. // 4,、創(chuàng)建會話對象 21. _session = [[AVCaptureSession alloc] init]; 22. // 并設(shè)置會話采集率 23. _session.sessionPreset = AVCaptureSessionPreset1920x1080; 24. 25. // 5、添加元數(shù)據(jù)輸出流到會話對象 26. [_session addOutput:metadataOutput]; 27. 28. // 創(chuàng)建攝像數(shù)據(jù)輸出流并將其添加到會話對象上, --> 用于識別光線強弱 29. self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init]; 30. [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; 31. [_session addOutput:_videoDataOutput]; 32. 33. // 6,、添加攝像設(shè)備輸入流到會話對象 34. [_session addInput:deviceInput]; 35. 36. // 7,、設(shè)置數(shù)據(jù)輸出類型(如下設(shè)置為條形碼和二維碼兼容),需要將數(shù)據(jù)輸出添加到會話后,,才能指定元數(shù)據(jù)類型,,否則會報錯 37. metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; 38. 39. // 8、實例化預(yù)覽圖層, 用于顯示會話對象 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]; 二、添加一對一直播系統(tǒng)開發(fā)源碼掃描涂層,,設(shè)置掃描蒙版,檢測邊框,、鏤空,、二維碼圖標(biāo)的四個角角落。 //懵層 - (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; } 三,、掃描完成,,對掃描結(jié)果進行分析和處理。一般一對一直播源碼的掃描結(jié)果分為兩種。 1,、掃描結(jié)果分析成功,,跳轉(zhuǎn)相關(guān)頁面 2、掃描結(jié)果解析失敗,,顯示暫未識別出掃描結(jié)果,。 - (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(@"暫未識別出掃描的二維碼"); } } 以上就是一對一直播源碼開發(fā)的掃描二維碼功能的大體流程實現(xiàn),該功能對于提高用戶感受和方便用戶使用都有幫助,,在萬物皆可掃一掃的時代背景下,,開發(fā)這個功能能夠加強一對一直播源碼開發(fā)增強社交性、互動性,,滿足人們的社交需求,。 |
|
來自: 昵稱70678147 > 《待分類》