在视频直播源码开发过程中,有时会需要用到二维码扫描功能,用户只要点开系统自带的摄像头,就能够扫描二维码,那么,这一功能该如何去实现呢?
一、 视频直播源码实现摄像头扫描
if (!_scanView) {
_scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight)];
_scanView.scanImageName = @"SGQRCode.bundle/QRCodeScanLineGrid";
_scanView.scanAnimationStyle = ScanAnimationStyleGrid;
_scanView.cornerLocation = CornerLoactionOutside;
_scanView.cornerColor = [UIColor orangeColor];
}
YBWeakSelf;
SGQRCodeObtainConfigure *configure = [SGQRCodeObtainConfigure QRCodeObtainConfigure];
configure.sampleBufferDelegate = YES;
[obtain establishQRCodeObtainScanWithController:self configure:configure];
[obtain setBlockWithQRCodeObtainScanResult:^(SGQRCodeObtain *obtain, NSString *result) {
if (result) {
[obtain stopRunning];
[obtain playSoundName:@"SGQRCode.bundle/sound.caf"];
NSLog(@"===%@",result);
NSArray *resA = [result componentsSeparatedByString:@"&"];
NSArray *roomA = [NSArray array];
NSArray *channelA = [NSArray array];
NSLog(@"resA:%@",resA);
for (NSString *needStr in resA) {
if ([needStr containsString:@"roomid"]) {
roomA = [needStr componentsSeparatedByString:@"="];
NSLog(@"roomA:%@",roomA);
}else if([needStr containsString:@"channel"]){
channelA = [needStr componentsSeparatedByString:@"="];
NSLog(@"roomA:%@",channelA);
}
}
if (roomA.count<2 ||channelA.count<2) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showErrorInfo:@[YZMsg(@"信息错误"),@"QRScan"]];
});
return ;
}
[weakSelf qrReset:@{@"id":roomA[1],@"channel":channelA[1]}];
}
}];
[obtain setBlockWithQRCodeObtainScanBrightness:^(SGQRCodeObtain *obtain, CGFloat brightness) {
if (brightness < - 1) {
[weakSelf.view addSubview:weakSelf.flashlightBtn];
} else {
if (weakSelf.isSelectedFlashlightBtn == NO) {
[weakSelf removeFlashlightBtn];
}
}
}];
二、在相册中进行选择
从相册中选择二维码后,视频直播源码应当有自助判断能力,确认该图片中是否具备二维码,超时未识别出二维码时,应当做出反馈
YBWeakSelf;
[obtain establishAuthorizationQRCodeObtainAlbumWithController:nil];
if (obtain.isPHAuthorization == YES) {
[self.scanView removeTimer];
}
[obtain setBlockWithQRCodeObtainAlbumDidCancelImagePickerController:^(SGQRCodeObtain *obtain) {
[weakSelf.view addSubview:weakSelf.scanView];
}];
[obtain setBlockWithQRCodeObtainAlbumResult:^(SGQRCodeObtain *obtain, NSString *result) {
if (result == nil) {
NSLog(@"暂未识别出二维码");
[MBProgressHUD showPop:@"未能识别"];
} else {
NSLog(@"===res:%@",result);
NSArray *resA = [result componentsSeparatedByString:@"&"];
NSArray *roomA = [NSArray array];
NSArray *channelA = [NSArray array];
NSLog(@"resA:%@",resA);
for (NSString *needStr in resA) {
if ([needStr containsString:@"roomid"]) {
roomA = [needStr componentsSeparatedByString:@"="];
NSLog(@"roomA:%@",roomA);
}else if([needStr containsString:@"channel"]){
channelA = [needStr componentsSeparatedByString:@"="];
NSLog(@"roomA:%@",channelA);
}
}
if (roomA.count<2 ||channelA.count<2) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showErrorInfo:@[YZMsg(@"信息错误"),@"QRScan"]];
});
return ;
}
[weakSelf qrReset:@{@"id":roomA[1],@"channel":channelA[1]}];
}
}];
三、销毁移除二维码
在使用完后,该二维码无需被保留,视频直播源码中应当编辑一串代码保障用户信息不受侵犯,以下是销毁移除的代码:
[self.scanView removeTimer];
[self removeFlashlightBtn];
[obtain stopRunning];