iOS读取制作二维码

iOS读取制作二维码



代码例子下载地址 "读取制作二维码.zip"



http://vdisk.weibo.com/s/Gbaa1



工程中需要的第三方库在代码例子中有的



我们来看看.h文件:

#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
@interface ViewController : UIViewController<ZBarReaderDelegate,UIAlertViewDelegate>
@property (strong , nonatomic) UIImageView* qRImageView;
@property (strong , nonatomic) ZBarReaderViewController* reader;
@property (strong , nonatomic) NSString* qRUrl;
-(IBAction)QRPress:(id)sender;
-(IBAction)qrBtnPress:(id)sender;
@end




.m文件如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.qRUrl = [[NSString alloc] init];
    //从这以下到32行
    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];
}





//制作二维码
-(IBAction)qrBtnPress:(id)sender
{
    /*字符转二维码
     导入 libqrencode文件
     引入头文件#import "QRCodeGenerator.h" 即可使用
     */
    self.qRImageView.image = [QRCodeGenerator qrImageForString:@"asd户外" imageSize:self.qRImageView.bounds.size.width];
}




//点击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<NSFastEnumeration> 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:^{
                                  
    }];
}


你可能感兴趣的:(ios,ZBarSDK,二维码制作与读取)