二维码的使用已是非常广泛了,不多说,直接入主题:
1.扫描二维码
在ios中扫描二维码的开源库常见的有ZBar和ZXing,在网上看到说ZXing在ios中只能实现扫描二维码,不支持一维码,所以,我就只用ZBar开源库了(有兴趣的也可以尝试ZXing),可以到github去下载,下载地址:https://github.com/bmorton/ZBarSDK。
下载之后,将整个库拖拽到你的工程之中,如下所示:
然后往项目中添加Framework框架及链接库(动态库、静态库),添加如下:AVFoundation.framwork, CoreMedia.framework, CoreVideo.framework, libiconv.dylib。
引入头文件:#import "ZBarSDK.h"。
-(void)scanQRcode:(id)sender { /*扫描二维码部分: 导入ZBarSDK文件并引入一下框架 AVFoundation.framework CoreMedia.framework CoreVideo.framework QuartzCore.framework libiconv.dylib 引入头文件#import “ZBarSDK.h” 即可使用 当找到条形码时,会执行代理方法 - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 最后读取并显示了条形码的图片和内容。*/ ZBarReaderViewController *readerVC = [[ZBarReaderViewController alloc] init]; readerVC.readerDelegate = self; readerVC.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = readerVC.scanner; [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; [self presentModalViewController:readerVC animated:YES]; [readerVC release]; }
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) break; imageView.image = [info objectForKey: UIImagePickerControllerOriginalImage]; [reader dismissModalViewControllerAnimated: YES]; resultLabel.text = symbol.data ; }
-(void)createQRcode:(id)sender { /*字符转二维码 导入 libqrencode文件 引入头文件#import "QRCodeGenerator.h" 即可使用 */ imageView.image = [QRCodeGenerator qrImageForString:field.text imageSize:imageView.bounds.size.width]; }