本章将讲解如何实现二维码扫描的功能
首先在github上下载ZBar SDK
地址https://github.com/bmorton/ZBarSDK
然后将如下的相关类库添加进去
AVFoundation.framwork, CoreMedia.framework, CoreVideo.framework, libiconv.dylib 和libzbar.a
接下来首先在.h文件中引用
#import "ZBarSDK.h"
同时添加代理
ZBarReaderDelegate
然后在.m文件中添加如下代码
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
1 -(void)sysbutbuttonclick{ 2 3 ZBarReaderViewController *reader = [ZBarReaderViewController new]; 4 reader.readerDelegate = self; 5 ZBarImageScanner *scanner = reader.scanner; 6 7 [scanner setSymbology: ZBAR_I25 8 config: ZBAR_CFG_ENABLE 9 to: 0]; 10 [self presentModalViewController: reader animated: YES]; 11 } 12 13 - (void) imagePickerController: (UIImagePickerController*) reader 14 didFinishPickingMediaWithInfo: (NSDictionary*) info 15 { 16 17 NSLog(@"info=%@",info); 18 // 得到条形码结果 19 id<NSFastEnumeration> results =[info objectForKey: ZBarReaderControllerResults]; 20 ZBarSymbol *symbol = nil; 21 for(symbol in results) 22 // EXAMPLE: just grab the first barcode 23 break; 24 25 26 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"信息提示!" message:symbol.data delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; 27 [alert show]; 28 // 将获得到条形码显示到我们的界面上 29 // resultText.text = symbol.data; 30 NSLog(@"%@",symbol.data); 31 // 扫描时的图片显示到我们的界面上 32 // resultImage.image = 33 // [info objectForKey: UIImagePickerControllerOriginalImage]; 34 35 // 扫描界面退出 36 //[reader dismissModalViewControllerAnimated: YES]; 37 }
然后需要触发扫描事件的时候 就调用
sysbutbuttonclick 就可以 了