• iOS之二维码的制作与扫描


    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:^{
           
        }];

    }
  • 相关阅读:
    stringstream复用【原创】
    C++访问权限【原创】
    C++进阶阅读
    程序员的自我修养-装载、链接与库【原创】
    WinDebug 常用命令表【摘】
    重要说明与访问必看
    Beyond Compare V3.2.3 Beta 中文版
    Batch File Rename Utility(文件批量改名软件) 1.1.4231
    Defraggler(磁盘整理软件) V2.21.993 绿色版
    DiskGenius(磁盘分区/数据恢复) 32位 V4.9.1 免费绿色版
  • 原文地址:https://www.cnblogs.com/yu0806/p/4302078.html
Copyright © 2020-2023  润新知