iOS Zbar掃描二維碼

?添加依賴庫:AVFoundation.framework验懊、libiconv.2.dylib、CoreMedia.framework、CoreVideo.framework,其中l(wèi)ibzbar.a是SDK中的,(如果需要通過SVN上傳自己代碼時丐箩,要記得還有這個.a文件)

在你要進(jìn)行添加掃描的界面的.h里可直接復(fù)制到工程里

添加頭文件

import "ZBarSDK.h"

//設(shè)置代理

@protocol ErweimaDelegate

//掃描出結(jié)果則調(diào)用這個方法

-(void)finishRead:(NSString *)barCode;

@end

@interface ErweimaViewController : superVC < ZBarReaderViewDelegate > //此處superVC是自定義的父類

@property (strong, nonatomic)id < ErweimaDelegate >delegate;

@end

4.在.m文件中:

@interface ErweimaViewController ()

{

ZBarReaderViewrederView;//二維碼掃描View,可自定義樣式

UIImageViewlineView;//自定義的掃描線

NSTimer *time;//定時器恤煞,控制掃描線跑動

}

//在進(jìn)入界面時就開始掃描

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];[rederView start];

}

//退出界面時就停止掃描

(void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

[rederView stop];

}

-(void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = @"掃描";

[self initView];

}

(void)initView

{

rederView = [[ZBarReaderView alloc] init];

rederView.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);

rederView.readerDelegate = self;

rederView.allowsPinchZoom = NO; // 不使用Pinch手勢變焦

rederView.torchMode = 0;//閃光燈 關(guān)

rederView.trackingColor = [UIColor clearColor];//鎖定圖碼 顯示的顏色框

if (TARGET_IPHONE_SIMULATOR) {

ZBarCameraSimulatorcamera = [[ZBarCameraSimulator alloc] initWithViewController:self];

camera.readerView = rederView;

}

[self.view addSubview:rederView];

/************可忽略此域中的以下代碼案站,可以自己自定義***************/

//掃描區(qū)域計算

CGRect scanMaskRect = CGRectMake(Width_Screen/2-75, 150, 150, 150);

CGRect scanCrop=[self getScanCrop:scanMaskRect readerViewBounds:rederView.bounds];

rederView.scanCrop = scanCrop;

[rederView.captureReader captureFrame];

[rederView start];

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(-20, -64, Width_Screen+40, Height_Screen)];

image.image = [UIImage imageNamed:@"111111111"];

image.backgroundColor = [UIColor clearColor];

image.alpha = 0.3;

[self.view addSubview:image];

//這里添加了類似微信的掃描線梯影,并開始上線滑動動畫

lineView=[[UIImageView alloc] initWithFrame:CGRectMake(51, 200, 219, 3)];

// lineView.image=[UIImage imageNamed:@"scan_line"];

lineView.backgroundColor = UIColor_Theme;

[self.view addSubview:lineView];

[self loadAnimationStart];

}

-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{

CGFloat x,y,width,height;

x = rect.origin.y / rvBounds.size.height;

y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;

width = (rect.origin.y + rect.size.height) / rvBounds.size.height;

height = 1 - rect.origin.x / rvBounds.size.width;

return CGRectMake(x, y, width, height);

}

-(void)loadAnimationStart{

if(Width_Screen !=320){? ? ? ? [UIViewanimateWithDuration:2.0animations:^{? ? ? ? lineView.frame=CGRectMake(lineView.frame.origin.x,390,lineView.frame.size.width,lineView.frame.size.height);? ? }completion:^(BOOL finished) {? ? ? ? ? ? ? time = [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(secStart)userInfo:nilrepeats:NO];? ? }];}else{? ? ? ? [UIViewanimateWithDuration:2.0animations:^{? ? ? ? lineView.frame=CGRectMake(lineView.frame.origin.x,345,lineView.frame.size.width,lineView.frame.size.height);? ? }completion:^(BOOL finished) {? ? ? ? ? ? ? ? time = [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(secStart)userInfo:nilrepeats:NO];? ? }];? ? }

}

-(void)secStart{

if(Width_Screen !=320){? ? lineView.frame=CGRectMake(lineView.frame.origin.x,175,lineView.frame.size.width,lineView.frame.size.height);if(time !=nil) {? ? ? ? [selfloadAnimationStart];? ? }? ? }else{? ? lineView.frame=CGRectMake(lineView.frame.origin.x,130,lineView.frame.size.width,lineView.frame.size.height);if(time !=nil) {? ? ? ? [selfloadAnimationStart];NSLog(@"123");? ? }NSLog(@"qwe");}

}

-(void)readerView:(ZBarReaderView)readerView didReadSymbols:(ZBarSymbolSet)symbols fromImage:(UIImage)image

{

NSStringstr;//掃描出的字段席噩,記錄下來

for(ZBarSymbol *symbolinsymbols) {NSLog(@"%@", symbol.data);? ? str=symbol.data;? ? [rederView stop];? ? [self.navigationController popViewControllerAnimated:YES];? ? [selfshowHudMessageAndHidden:str];//展示出掃描結(jié)果勋拟,根據(jù)需求可自定義展示樣式lineView.hidden =YES;? ? [time setFireDate:[NSDatedistantFuture]];? ? [time invalidate];? ? time =nil;break;}//這個代理是 返回到上個界面把str傳過去的,根據(jù)需求可改變if([_delegate respondsToSelector:@selector(finishRead:)]){? ? ? ? [_delegate finishRead:str];}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末苔货,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子立哑,更是在濱河造成了極大的恐慌夜惭,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铛绰,死亡現(xiàn)場離奇詭異诈茧,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)捂掰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門敢会,熙熙樓的掌柜王于貴愁眉苦臉地迎上來曾沈,“玉大人,你說我怎么就攤上這事鸥昏∪悖” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵吏垮,是天一觀的道長障涯。 經(jīng)常有香客問我,道長膳汪,這世上最難降的妖魔是什么唯蝶? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮遗嗽,結(jié)果婚禮上粘我,老公的妹妹穿的比我還像新娘。我一直安慰自己痹换,他們只是感情好涂滴,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著晴音,像睡著了一般柔纵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锤躁,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天搁料,我揣著相機(jī)與錄音,去河邊找鬼系羞。 笑死郭计,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的椒振。 我是一名探鬼主播昭伸,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼澎迎!你這毒婦竟也來了庐杨?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤夹供,失蹤者是張志新(化名)和其女友劉穎灵份,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哮洽,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡填渠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氛什。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡莺葫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出枪眉,到底是詐尸還是另有隱情捺檬,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布瑰谜,位于F島的核電站欺冀,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏萨脑。R本人自食惡果不足惜隐轩,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望渤早。 院中可真熱鬧职车,春花似錦、人聲如沸鹊杖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骂蓖。三九已至积瞒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間登下,已是汗流浹背茫孔。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留被芳,地道東北人缰贝。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像畔濒,于是被迫代替她去往敵國和親剩晴。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容