二維碼掃描思路:獲取二維碼內部信息 + 處理剛剛獲取到的內部信息
頭文件:#import "ZBarSDK.h"
<!-- 相機 -->
<key>NSCameraUsageDescription</key>
<string>APP需要您的同意,才能訪問相機</string>
參《iOS10之后 權限設置》
宏定義:#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) //屏寬
A.直接使用ZBarReaderViewController:
協議:ZBarReaderDelegate
UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(30,70, 150,35)];
btn.backgroundColor = [UIColor lightGrayColor];
[btn setTitle:@"點擊,掃描" 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;//顯示控制項
readerVC.tracksSymbols = YES; //顯示追蹤框
//設置識別范圍
//(距離左邊/寬度,距離上邊/高度,識別寬度/寬度,識別高度/高度)
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);//掃描范圍:屏幕大小
//設置識別的參數 根據需求調整,可以提高識別速度。
ZBarImageScanner *scanner = readerVC.scanner;
[scanner setSymbology:ZBAR_I25 //此參數和“to"后面的參數配合 確定了識別的編碼范圍
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);//打印識別的數據
[reader dismissViewControllerAnimated:YES completion:^{
}];
}
效果:
初次,會提示“訪問相機權限:
初次,會提示“訪問相機權限”
之后,可以掃描
返回結果:
2017-09-12 21:41:48.586385 scanVC[3952:1369923] http://r.m.baidu.com/3ii99ns
若獲取到的二維碼內部信息是一些參數,需要處理這些參數!這就是不同APP的處理了!
Demo:獲取二維碼信息
真機測試,參考:《開發者賬號 真機測試》、《個人賬號 真機測試》
B.繼承ZBarReaderViewController
繼承成功的“ScanQRViewController.h
”內部:
#import <ZBarSDK/ZBarSDK.h> //頭文件
@interface ScanQRViewController : ZBarReaderViewController
@end
ScanQRViewController * readerQR_VC = [[ScanQRViewController alloc] init];
readerQR_VC.title = @"XXXXX";
readerQR_VC.readerDelegate = self;
//設置識別的參數 根據需求調整,可以提高識別速度。
ZBarImageScanner * scanner = readerQR_VC.scanner;
[scanner setSymbology:ZBAR_I25 //此參數和“to"后面的參數配合 確定了識別的編碼范圍
config:ZBAR_CFG_ENABLE
to:0];
[self.navigationController pushViewController:readerQR_VC animated:YES];
協議方法:里面處理掃描到的信息!
#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);//打印識別的數據
[reader.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//顯示狀態欄
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showsZBarControls = NO; //隱藏控制項
self.tracksSymbols = NO; //隱藏追蹤框
//設置識別范圍(距離左邊/寬度,距離上邊/高度,識別寬度/寬度,識別高度/高度)
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;
//掃描動畫 (根據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];//重復次數
[lineImgV setFrame:CGRectMake(0, 0+width, width, 3)]; //最終位置
[UIView commitAnimations];
}
效果:
由于獲取二維碼信息這一塊,已經被實現了!我們的工作就只有處理 二維碼信息了!
#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);//打印識別的數據 //[reader.navigationController popViewControllerAnimated:YES]; }
即:根據我們APP的需要,對獲取的信息(symbol.data)進行處理、封裝!
閃光燈
在- (void)viewDidLoad { }
里面:
//閃光燈按鈕
_flashLightBtn = [[UIButton alloc] init];
[_flashLightBtn addTarget:self action:@selector(openOrCloseFlashLight) forControlEvents:UIControlEventTouchUpInside];
[_flashLightBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//字體顏色
[_flashLightBtn setTitle:@"閃光燈:打開" forState:UIControlStateNormal];//正常狀態標題
[_flashLightBtn setTitle:@"閃光燈:關閉" forState:UIControlStateSelected];//選中狀態標題
[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);
}];
//關閉閃光燈(進入后)
readerView.torchMode = 0;
-(void)openOrCloseFlashLight {
if (_flashLightBtn.selected == YES) {
//當前:點亮狀態
readerView.torchMode = 0;//關閉
_flashLightBtn.selected = NO;
} else { //當前:關閉狀態
readerView.torchMode = 1;//打開
_flashLightBtn.selected = YES;
}
}
效果:
閃光燈