IOS之二维码的制作与扫描
二维码SDK的Dome“QRCode”中制作libqrencode库文件,扫描ZBarSDK库文件
1.制作二维码 /*字符转二维码
导入 libqrencode文件
添加 #import "QRCodeGenerator.h"
@property (strong , nonatomic) UIImageView* qRImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.qRImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
[self.view addSubview:self.qRImageView];
}
-(IBAction)qrBtnPress:(id)sender
{
self.qRImageView.image = [QRCodeGenerator qrImageForString:@"二维码存储的字符串信息 asddsdd" imageSize:self.qRImageView.bounds.size.width];
}
2.读取二维码信息
添加#import "ZBarSDK.h"//读二维码库
@interface ViewController : UIViewController//读二维码的代
@property (strong , nonatomic) ZBarReaderViewController* reader;
@property (strong , nonatomic) NSString* qRUrl;
- (void)viewDidLoad
{
[super viewDidLoad];
self.qRUrl = [[NSString alloc] init];
self.reader = [[ZBarReaderViewController alloc] init];
self.reader.readerDelegate = self;
self.reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = self.reader.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
//这部分为zBarSDK文档提供的例子给出的主要的意思就是初始化ZBarReaderViewController类的对象,以及设置代理回调方法为 - (void) imagePickerController: (UIImagePickerController*) reader
//didFinishPickingMediaWithInfo: (NSDictionary*) info
self.qRImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
[self.view addSubview:self.qRImageView];
}
//点击Button时将扫描二维码需要的照相机视图, self.reader是(ZBarReaderViewController)类的对象
-(IBAction)QRPress:(id)sender
{
[self presentViewController:self.reader animated:YES completion:^{
NSLog(@"fd");
}];
}
//这个回调方法的写法是固定,只要知道以下几点就好需要的数据是从symbol.data中取出的,因此也就需要在.h文件中声明一个字符串接受就好。在这里我在.h中声明了self.qRUrl对象进行接受,之后只要按下面的格式写就好。
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
id results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
{
NSLog(@"symbol =%@",symbol.data);
break;
}
self.qRUrl = symbol.data;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.qRUrl message:self.qRUrl delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
self.qRImageView.image =
[info objectForKey: UIImagePickerControllerOriginalImage];
[self.reader dismissViewControllerAnimated:YES completion:^{
}];
}