iOS中使用ZBar掃描二維碼自定義掃描界面功能
之前在A(yíng)ndroid中使用過(guò)ZXing識(shí)別二維碼,ZXing也有對(duì)應(yīng)的iOS版本,經(jīng)過(guò)了解,ZBar也是一個(gè)常用的二維碼識(shí)別軟件,并分別提供了iOS和Android的SDK可供使用,最終我選擇了ZBar進(jìn)行二維碼識(shí)別,它的注釋清晰,便于使用。
ZBar為我們提供了兩種使用方式,一種是直接調(diào)用ZBar提供的ZBarReaderViewController打開(kāi)一個(gè)掃描界面,另一種方式是使用ZBar提供的可以嵌在其他視圖中的ZBarReaderView,實(shí)際項(xiàng)目中我們更可能會(huì)使用第二種方式,這可以讓我們對(duì)界面做更多的定制。
ZBar使用起來(lái)也非常簡(jiǎn)單,將ZBarSDK導(dǎo)入項(xiàng)目,在需要使用ZBar的文件中導(dǎo)入ZBarSDK.h頭文件即可
#pragma mark 初始化掃描
- (void)InitScan
{
readview = [ZBarReaderView new];
readview.backgroundColor = [UIColor clearColor];
readview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
readview.readerDelegate = self;
readview.allowsPinchZoom = YES;//使用手勢(shì)變焦
readview.trackingColor = [UIColor redColor];
readview.showsFPS = NO;// 顯示幀率 YES 顯示 NO 不顯示
//readview.scanCrop = CGRectMake(0, 0, 1, 1);//將被掃描的圖像的區(qū)域
UIImage *hbImage=[UIImage imageNamed:@"pick_bg.png"];
scanZomeBack=[[UIImageView alloc] initWithImage:hbImage];
//添加一個(gè)背景圖片
CGRect mImagerect=CGRectMake((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0, 200, 200);
[scanZomeBack setFrame:mImagerect];
readview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:readview.bounds];//將被掃描的圖像的區(qū)域
[readview addSubview:scanZomeBack];
[readview addSubview:readLineView];
[self.view addSubview:readview];
[readview start];
}
#pragma mark 獲取掃描區(qū)域
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
CGFloat x,y,width,height;
x = rect.origin.x / readerViewBounds.size.width;
y = rect.origin.y / readerViewBounds.size.height;
width = rect.size.width / readerViewBounds.size.width;
height = rect.size.height / readerViewBounds.size.height;
return CGRectMake(x, y, width, height);
}
#pragma mark 掃描動(dòng)畫(huà)
-(void)loopDrawLine
{
CGRect rect = CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y, scanZomeBack.frame.size.width, 2);
if (readLineView) {
[readLineView removeFromSuperview];
}
readLineView = [[UIImageView alloc] initWithFrame:rect];
[readLineView setImage:[UIImage imageNamed:@"line.png"]];
[UIView animateWithDuration:3.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
//修改fream的代碼寫(xiě)在這里
readLineView.frame =CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height, scanZomeBack.frame.size.width, 2);
[readLineView setAnimationRepeatCount:0];
}
completion:^(BOOL finished){
if (!is_Anmotion) {
[self loopDrawLine];
}
}];
[readview addSubview:readLineView];
}
#pragma mark 獲取掃描結(jié)果
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
// 得到掃描的條碼內(nèi)容
const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {
// 是否QR二維碼
}
for (ZBarSymbol *symbol in symbols) {
[sTxtField setText:symbol.data];
break;
}
[readerView stop];
[readerView removeFromSuperview];
}
github地址:https://github.com/ZBar/ZBar
以上所述是小編給大家介紹的iOS中使用ZBar掃描二維碼自定義掃描界面,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化
本篇文章主要介紹了使用jquery.i18n.properties 實(shí)現(xiàn)web前端國(guó)際化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例
這篇文章主要介紹了iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10
iOS實(shí)現(xiàn)百度外賣(mài)頭像波浪的效果
對(duì)于現(xiàn)在很多人來(lái)說(shuō),叫外賣(mài)就成了不可或缺的習(xí)慣。某日瞬間發(fā)現(xiàn)百度外賣(mài)的APP波浪效果很是吸引人,相比較其他的外賣(mài)APP,顏值略高些.(淘寶也有波浪的效果),遂就思考如何實(shí)現(xiàn)這種"浪"的效果,下面來(lái)一起看看。2016-08-08
iOS中的應(yīng)用啟動(dòng)原理以及嵌套模型開(kāi)發(fā)示例詳解
這篇文章主要介紹了iOS中的應(yīng)用啟動(dòng)原理以及嵌套模型開(kāi)發(fā)示例詳解,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能詳解
查看大圖是們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)需求,下面這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-09-09

