二維碼掃描思路:獲取二維碼內(nèi)部信息 + 處理剛剛獲取到的內(nèi)部信息
頭文件:#import "ZBarSDK.h"
<!-- 相機(jī) -->
<key>NSCameraUsageDescription</key>
<string>APP需要您的同意,才能訪(fǎng)問(wèn)相機(jī)</string>
參《iOS10之后 權(quán)限設(shè)置》
宏定義:#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) //屏寬
A.直接使用ZBarReaderViewController:
協(xié)議:ZBarReaderDelegate
當(dāng)前視圖控制器中吁津,添加一個(gè)按鈕悔橄。點(diǎn)擊跳到下一個(gè)界面:
UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(30,70, 150,35)];
btn.backgroundColor = [UIColor lightGrayColor];
[btn setTitle:@"點(diǎn)擊,掃描" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickToPresentZbarView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
在“-(void)clickToPresentZbarView
”里:
ZBarReaderViewController * readerVC = [[ZBarReaderViewController alloc] init];
readerVC.readerDelegate = self;
readerVC.showsZBarControls = YES;//顯示控制項(xiàng)
readerVC.tracksSymbols = YES; //顯示追蹤框
//設(shè)置識(shí)別范圍
//(距離左邊/寬度腺毫,距離上邊/高度癣疟,識(shí)別寬度/寬度,識(shí)別高度/高度)
float width = SCREEN_WIDTH*500.f/750.f; //掃描寬度:屏幕寬度的2/3
float height = SCREEN_WIDTH*500.f/750.f; //掃描寬度:屏幕寬度的2/3
float scanV_X = (SCREEN_WIDTH-width)/2.f;
float scanV_Y = (SCREEN_HEIGHT-height)/2.f;
CGRect scanViewRect = CGRectMake(scanV_X/SCREEN_WIDTH, scanV_Y/SCREEN_HEIGHT, width/SCREEN_WIDTH, height/SCREEN_HEIGHT);
readerVC.scanCrop = scanViewRect;
//readerVC.scanCrop = CGRectMake(0, 0, 1, 1);//掃描范圍:屏幕大小
//設(shè)置識(shí)別的參數(shù) 根據(jù)需求調(diào)整潮酒,可以提高識(shí)別速度睛挚。
ZBarImageScanner *scanner = readerVC.scanner;
[scanner setSymbology:ZBAR_I25 //此參數(shù)和“to"后面的參數(shù)配合 確定了識(shí)別的編碼范圍
config:ZBAR_CFG_ENABLE
to:0];
[self presentViewController:readerVC animated:YES completion:nil];
#pragma mark - ZBarReaderDelegate
-(void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info {
id<NSFastEnumeration> results =[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol =nil;
for(symbol in results)
break;
NSLog(@"%@",symbol.data);//打印識(shí)別的數(shù)據(jù)
[reader dismissViewControllerAnimated:YES completion:^{
}];
}
效果:
初次,會(huì)提示“訪(fǎng)問(wèn)相機(jī)權(quán)限:
初次急黎,會(huì)提示“訪(fǎng)問(wèn)相機(jī)權(quán)限”
之后扎狱,可以?huà)呙?/p>
返回結(jié)果:
2017-09-12 21:41:48.586385 scanVC[3952:1369923] http://r.m.baidu.com/3ii99ns
當(dāng)然污抬,這種掃描的信息是 可以直接加載的網(wǎng)址!
若獲取到的二維碼內(nèi)部信息是一些參數(shù)绳军,需要處理這些參數(shù)印机!這就是不同APP的處理了!
Demo:獲取二維碼信息
真機(jī)測(cè)試门驾,參考:《開(kāi)發(fā)者賬號(hào) 真機(jī)測(cè)試》射赛、《個(gè)人賬號(hào) 真機(jī)測(cè)試》
B.繼承ZBarReaderViewController
繼承成功的“ScanQRViewController.h
”內(nèi)部:
#import <ZBarSDK/ZBarSDK.h> //頭文件
@interface ScanQRViewController : ZBarReaderViewController
@end
ScanQRViewController * readerQR_VC = [[ScanQRViewController alloc] init];
readerQR_VC.title = @"XXXXX";
readerQR_VC.readerDelegate = self;
//設(shè)置識(shí)別的參數(shù) 根據(jù)需求調(diào)整,可以提高識(shí)別速度奶是。
ZBarImageScanner * scanner = readerQR_VC.scanner;
[scanner setSymbology:ZBAR_I25 //此參數(shù)和“to"后面的參數(shù)配合 確定了識(shí)別的編碼范圍
config:ZBAR_CFG_ENABLE
to:0];
[self.navigationController pushViewController:readerQR_VC animated:YES];
協(xié)議方法:里面處理掃描到的信息楣责!
#pragma mark - ZBarReaderDelegate
-(void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info {
id<NSFastEnumeration> results =[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol =nil;
for(symbol in results)
break;
NSLog(@"%@",symbol.data);//打印識(shí)別的數(shù)據(jù)
[reader.navigationController popViewControllerAnimated:YES];
}
在“ScanQRViewController.m
”內(nèi)部:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//顯示狀態(tài)欄
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showsZBarControls = NO; //隱藏控制項(xiàng)
self.tracksSymbols = NO; //隱藏追蹤框
//設(shè)置識(shí)別范圍(距離左邊/寬度竣灌,距離上邊/高度,識(shí)別寬度/寬度秆麸,識(shí)別高度/高度)
float width = SCREEN_WIDTH*500.f/750.f;
float height = SCREEN_WIDTH*500.f/750.f;
float scanV_X = (SCREEN_WIDTH-width)/2.f;
float scanV_Y = (SCREEN_HEIGHT-height)/2.f;
CGRect scanViewRect = CGRectMake(scanV_X/SCREEN_WIDTH, scanV_Y/SCREEN_HEIGHT, width/SCREEN_WIDTH, height/SCREEN_HEIGHT);
self.scanCrop = scanViewRect;
[self setMaskViewAndScanAnimation];
}
-(void)setMaskViewAndScanAnimation {
//掩蓋視圖maskView
UIView * maskView = [[UIView alloc] initWithFrame:SCREEN_Bounds];
maskView.backgroundColor = [UIColor clearColor];
self.cameraOverlayView = maskView;//添加蒙版視圖
//掃描范圍
float width = SCREEN_WIDTH*500.f/750.f;
float height = SCREEN_WIDTH*500.f/750.f;
UIView * scanBoxV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
scanBoxV.backgroundColor = [UIColor clearColor];//透明色才能掃描
scanBoxV.center = maskView.center;
[maskView addSubview:scanBoxV];
scanBoxV.layer.borderWidth = 2.f;
scanBoxV.layer.borderColor = ChosedColor.CGColor;
//掃描動(dòng)畫(huà) (根據(jù)UI需求:放圖片或試圖)
UIImageView *lineImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, 3)];
[scanBoxV addSubview:lineImgV];
lineImgV.image = [UIImage imageNamed:@"scan_line"];
[UIView beginAnimations:@"animationID" context:NULL];
[UIView setAnimationDuration:3.f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:lineImgV cache:YES];
[UIView setAnimationRepeatCount:100];//重復(fù)次數(shù)
[lineImgV setFrame:CGRectMake(0, 0+width, width, 3)]; //最終位置
[UIView commitAnimations];
}
效果:
由于獲取二維碼信息這一塊初嘹,已經(jīng)被實(shí)現(xiàn)了!我們的工作就只有處理 二維碼信息了蛔屹!
#pragma mark - ZBarReaderDelegate 二維碼信息獲取 -(void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info { id<NSFastEnumeration> results =[info objectForKey:ZBarReaderControllerResults]; ZBarSymbol *symbol =nil; for(symbol in results) break; NSLog(@"%@",symbol.data);//打印識(shí)別的數(shù)據(jù) //[reader.navigationController popViewControllerAnimated:YES]; }
即:根據(jù)我們APP的需要削樊,對(duì)獲取的信息(symbol.data)進(jìn)行處理豁生、封裝兔毒!
閃光燈
在- (void)viewDidLoad { }
里面:
//閃光燈按鈕
_flashLightBtn = [[UIButton alloc] init];
[_flashLightBtn addTarget:self action:@selector(openOrCloseFlashLight) forControlEvents:UIControlEventTouchUpInside];
[_flashLightBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//字體顏色
[_flashLightBtn setTitle:@"閃光燈:打開(kāi)" forState:UIControlStateNormal];//正常狀態(tài)標(biāo)題
[_flashLightBtn setTitle:@"閃光燈:關(guān)閉" forState:UIControlStateSelected];//選中狀態(tài)標(biāo)題
[maskView addSubview:_flashLightBtn];
[_flashLightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(maskView);
make.bottom.equalTo(maskView).with.offset(-SCREEN_WIDTH*30.f/750.f);
make.width.mas_equalTo(SCREEN_WIDTH*1.f/2.f);
make.height.mas_equalTo(SCREEN_WIDTH*60.f/750.f);
}];
//關(guān)閉閃光燈(進(jìn)入后)
readerView.torchMode = 0;
-(void)openOrCloseFlashLight {
if (_flashLightBtn.selected == YES) {
//當(dāng)前:點(diǎn)亮狀態(tài)
readerView.torchMode = 0;//關(guān)閉
_flashLightBtn.selected = NO;
} else { //當(dāng)前:關(guān)閉狀態(tài)
readerView.torchMode = 1;//打開(kāi)
_flashLightBtn.selected = YES;
}
}
效果: