自定义二维码扫描界面
有时需要在扫描界面显示应用程序的导航条和工具栏,而不是全屏都是扫描界面,此时就需要自定义扫描界面。
这时,用ZBarReaderView比较方便,比ZBarReaderViewcontroller方便控制界面。
1.将扫描视图加到当前需要扫描的界面上
ZBarReaderView *readerView = [[ZBarReaderView alloc]init];
readerView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height – 88);
ScanRegionView *scanView = [[ScanRegionView alloc] initWithFrame:CGRectMake(0, 0, 320, readerView.frame.size.height)];
scanView.backgroundColor = [UIColor clearColor];
readerView.readerDelegate = self;
[readerView addSubview:scanView];
//关闭闪光灯
readerView.torchMode = 0;
[self.view addSubview:readerView];
//扫描区域计算
readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
[readerView start];
2、实现委托ZBarReaderViewDelegate委托方法
ZBarReaderViewDelegate
-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
//判断是否包含 头\’http:\’
NSString *regex = @”http+:[^\\s]*”;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”,regex];
//判断是否包含 头\’ssid:\’
NSString *ssid = @”ssid+:[^\\s]*”;;
NSPredicate *ssidPre = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”,ssid];
if ([predicate evaluateWithObject:symbolStr]) {
}
else if([ssidPre evaluateWithObject:symbolStr]){
NSArray *arr = [symbolStr componentsSeparatedByString:@”;”];
NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@”:”];
NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@”:”];
symbolStr = [NSString stringWithFormat:@”ssid: %@ \n password:%@”,
[arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]];
UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];
//然后,可以使用如下代码来把一个字符串放置到剪贴板上:
pasteboard.string = [arrInfoFoot objectAtIndex:1];
}
}
这样就能按自己的想法来设置扫描界面了。我试过将中间一小块区域设置成扫描区域,周围全是半透明的背景层,相对于使用ZBarReaderViewcontroller,更容易实现。