ios二維碼掃描插件,適配當前主流掃描軟件,自定義掃描界面。

二維碼介紹:
 
 
 
二維碼(QR(Quick Response)code),又稱二維條碼,最早起源於日本。
 
它是用特定的幾何圖形按一定規律在平面(二維方向)上分佈的黑白相間的圖形,是所有信息數據的一把鑰匙。
 
二維碼是一種比一維碼更高級的條碼格式。一維碼只能在一個方向(一般是水平方向)上表達信息,
 
而二維碼在水平和垂直方向都可以存儲信息。一維碼只能由數字和字母組成,而二維碼能存儲漢字、數字和圖片等信息,
 
因此二維碼的應用領域要廣得多。
 
 
 
二維碼需求:
 
 
 
開發一款二維碼掃描插件,具有掃描大衆二維碼的能力,能夠識別二維碼當中包含的網頁鏈接以及文本信息。對網頁鏈接跳轉safari瀏覽器(但是對自己公司的連接要求在app內部內置瀏覽器)。對掃描的內容彈出提示框。並且讓別的app掃描我們自己的二維碼的時候要跳轉到appstore下載
 
 
 
需求確認:
 
二維碼算法自己寫?不現實。開發週期太長,那就使用第三方那個庫QR(Quick Response)code  ZBarSDK,讀取信息的時候首先應該判斷是不是網頁連接,考慮用正則表達式對掃描的結果進行過濾。對於內置瀏覽器使用webview控件。彈出提示框使用UIAlertView太麻煩,而且不好用,所以採用開源第三方庫BlockAlertActionSheet,BlockAlertActionSheet使用block做的一款具有提示功能類似UIAlertView  UIActionSheet等控件。很強大,很實用。
 
 
 
二維碼開發:
 
 
 
首先在github上下載ZBar SDK
 
地址https://github.com/bmorton/ZBarSDK
 
 
 
下載BlockAlertActionSheet,
 
新建一個test工程。在Main》storyboard上拖放一個button點擊button開始掃描。
 
爲button連接插座事件。
 
 
 
- (IBAction)btnClicked:(id)sender{
 
 
 
}
 
 
 
將ZBarSDK包含在項目工程當中。添加庫:QuartzCore.framework ,CoreVideo.framework ,CoreMedia.framework,libiconv.dylib,CoreGraphics.framework。
 
 
 
將BlockAlertActionSheet包含在項目工程當中
 
 
 
在 WSQViewController.h中引入頭文件。
 
 
 
#import "ZBarSDK.h"
 
#import "BlockAlertView.h"
 
遵循協議 ZBarReaderDelegate,
 
 
 
#pragma mark - ZBarReaderDelegate<UIImagePickerControllerDelegate>
 
- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
 
    withRetry: (BOOL) retry
 
    {
 
    }
 
    
 
    //二維碼
 
- (void) imagePickerController: (UIImagePickerController*) reader
 
    didFinishPickingMediaWithInfo: (NSDictionary*) info
 
    {
 
  
 
}
 
 
 
定義實例變量:
 
 
 
ZBarReaderViewController *reader;
 
UIView* line; //二維碼掃描線。
 
BOOL isBottom;
 
NSTimer* lineTimer;//二維碼掃描線計時器。
 
 
 
 
 
自定義二維碼掃描界面,(想法是這樣的,先把reader原來的界面全部清空,然後自定義界面,因爲ZBarSDK是分裝好的靜態庫,)
 
 
 
 
 
-(void)setOverlayStyle:(ZBarReaderViewController *)reader_{
 
for (UIView *temp in [reader_.view subviews]){
 
for (UIButton* btn in [temp subviews]) {
 
if ([btn isKindOfClass:[UIButton class]]) {
 
[btn removeFromSuperview];
 
}
 
}
 
//去掉toolbar
 
for (UIToolbar* tool in [temp subviews]) {
 
if ([tool isKindOfClass:[UIToolbar class]]) {
 
[tool setHidden:YES];
 
[tool removeFromSuperview];
 
}
 
}
 
isBottom = NO;
 
//掃描線
 
 
 
line = [[UIView alloc] initWithFrame:CGRectMake(40, 105, 240, 2)];
 
line.backgroundColor = [UIColor greenColor];
 
[reader_.view addSubview:line];
 
 
 
lineTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(moveLine) userInfo:nil repeats:YES];
 
[lineTimer fire];
 
 
 
UIImage *scanningBg = [UIImage imageNamed:@"scanning-568h.png"];
 
 
 
CGSize size  = [UIScreen mainScreen].bounds.size;
 
UIImageView *scanningView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
 
scanningView.image = scanningBg;
 
[reader_.view addSubview:scanningView];
 
 
 
 
 
//用於取消操作的button
 
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 
UIImage *bimage = [UIImage imageNamed:@"yellowButton.png"];
 
//[cancelButton setBackgroundImage:bimage forState:UIControlStateDisabled];
 
[cancelButton setBackgroundColor:[UIColor whiteColor]];
 
[cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
 
[cancelButton setFrame:CGRectMake(20, size.height - 84, 280, 40)];
 
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
 
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
 
[cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
 
 
 
[reader_.view addSubview:cancelButton];
 
 
 
}
 
}
 
 
 
//屏幕移動掃描線。
 
-(void)moveLine{
 
CGRect lineFrame = line.frame;
 
CGFloat y = lineFrame.origin.y;
 
if (!isBottom) {
 
isBottom = YES;
 
y=y+245.0;
 
lineFrame.origin.y = y;
 
[UIView animateWithDuration:1.5 animations:^{
 
line.frame = lineFrame;
 
}];
 
}else if(isBottom){
 
isBottom = NO;
 
y = y -245;
 
lineFrame.origin.y = y;
 
[UIView animateWithDuration:1.5 animations:^{
 
line.frame = lineFrame;
 
}];
 
}
 
}
 
 
 
// 點擊cancel  button事件
 
- (void)dismissOverlayView:(id)sender{
 
[lineTimer invalidate];
 
[reader dismissModalViewControllerAnimated:YES];
 
}
發佈了34 篇原創文章 · 獲贊 20 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章