美文网首页
点击按钮扫描二维码

点击按钮扫描二维码

作者: 本客 | 来源:发表于2018-12-20 20:32 被阅读0次

 本文实现是的效果是,点击按钮跳转到扫描二维码界面,扫描出来后跳转到二维码指定的网页,把二维码和网址分别展示在页面上

首先需要导入二维码SDK   ZBarSDK 2   如图所示:

想要真机运行,必须要设置如图这步操作

把工程中General中的Bundle Identifier 改成com.yxt.ZBarDemo

添加依赖库,如图所示:

在工程中info.plist中开网,然后添加访问相机的权限  Privacy - Camera Usage Description    后面跟上“使用您的相机以便扫描二维码,请您放心使用”  这样基本的配置就完成了

在ViewController.m中导入头文件#import "ZBarSDK 2/Headers/ZBarSDK/ZBarSDK.h"  注意有可能路径不同,但是最终导入的是ZBarSDK.h

宏定义   #define SCR_W [UIScreen mainScreen].bounds.size.width

              #define SCR_H [UIScreen mainScreen].bounds.size.height


定义属性:@property(nonatomic,strong)UILabel * showLabel;

                  @property(nonatomic,strong)UIImageView * showImgView;

                  @property(nonatomic,strong)UIButton * btn;

在- (void)viewDidLoad里面写以下代码

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    self.btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100,SCR_W-200, 40)];

    [self.btn setTitle:@"扫描二维码" forState:UIControlStateNormal];

    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.btn addTarget:self action:@selector(scanQRImage:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.btn];

    self.showLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 200, SCR_W-20, 100)];

    self.showLabel.backgroundColor=[UIColor lightGrayColor];

    self.showLabel.numberOfLines=0;

    [self.view addSubview:self.showLabel];

    self.showImgView=[[UIImageView alloc]initWithFrame:CGRectMake(60, 350, SCR_W-120,SCR_W-120 )];

    self.showImgView.backgroundColor=[UIColor redColor];

    [self.view addSubview:self.showImgView];

}

实现二维码的方法

-(void)scanQRImage:(UIButton* )sender

{

    // 实例化扫描控制器

    ZBarReaderViewController *readVC = [[ZBarReaderViewController alloc] init];

    // 设置代理

    readVC.readerDelegate=self;

    // 获取控制器中的扫描器对象

    ZBarImageScanner*scanner = readVC.scanner;

    // 设置扫描器识别的图片类型

    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];

    // 跳转至扫描控制器

    [self presentViewController:readVC animated:YES completion:nil];

}


遵守二维码对象的协议

<ZBarReaderDelegate>

二维码的扫描的回调方法

#pragma mark ---- ZBarReaderDelegate -----

// 获取到多媒体信息(图片、二维码、视频)后回调的方法

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // 隐藏当前的扫描控制器视图

    [picker dismissViewControllerAnimated:YES completion:nil];

    /*

     获取扫描到的图片数据

     */

    // 获取扫描到的图片数据

    UIImage*img =  [info objectForKey:UIImagePickerControllerOriginalImage];

    // 将图片显示在图像视图上

    self.showImgView.image= img;

    /*

     获取扫描得到的二维码图片信息

     */

    // 得到扫描到的图片信息数据枚举

    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];

    // 获取集合中的信息

    ZBarSymbol*symbol =nil;

    for(symbol in results) {

        break;

    }

    if(symbol ==nil) {

        NSLog(@"扫描失败");

        return;

    }

    // 如果不为空,获取扫描到的结果

    NSString*resStr = symbol.data;

    // 把数据展示在lable上

    self.showLabel.text= resStr;

    // 判断是否是一个网址字符串,是的话做app跳转

    if([symbol.data   hasPrefix:@"http://"] || [symbol.data  hasPrefix:@"https://"]) {

        //

        NSURL*url = [NSURL  URLWithString:symbol.data];

        [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];

    }

}

相关文章

  • 点击按钮扫描二维码

    本文实现是的效果是,点击按钮跳转到扫描二维码界面,扫描出来后跳转到二维码指定的网页,把二维码和网址分别展示在页面上...

  • 人民好医生iOS企业版App手机安装步骤

    1.扫描二维码后显示安装界面 2.点击【在Safari中打开】 3.点击安装 4.界面出现弹窗后,点击【安装】按钮...

  • iOS企业版App手机安装步骤

    1.扫描二维码后显示安装界面 2.点击【在Safari中打开】 3.点击安装 4.界面出现弹窗后,点击【安装】按钮...

  • 扫码登录前端梳理

    点击按钮会跳转到一个新页面, 扫描新页面二维码, 会自动跳转到 cmsUrl 的login 页面.cmsUrl 需...

  • iOS 安装企业应用流程

    1. 使用微信扫一扫功能,扫描二维码 2, 扫描完成后,跳转到该网页,然后点击右上脚的菜单按钮 3. 在底部弹窗中...

  • swift笔记(二)

    1.导航栏按钮 2.标题按钮 3.弹出菜单 4.通知 5.扫描二维码 6.生成二维码

  • 淘宝登录中点击二维码按钮

    在模拟淘宝登录中,想用扫描二维码的方式登录淘宝。登录页面有时呈现账号密码的登录方式,此时需要点击切换按钮(ID为J...

  • SaCa EMM 的安装方式

    Android系统扫描二维码,下载的就是apk的安装包,直接点击安装就可以。 iOS系统扫描二维码,下载的是描述文...

  • 扫码登录原理及测试要点

    操作流程: 打开登录页面,展示一个二维码(web) 打开APP扫描该二维码后,APP显示确认、取消按钮(app) ...

  • 如何引导现场用户积极参与微信大屏幕现场互动?

    活动主办方可在活动现场直播环节,点击页面上方二维码,弹出二维码扫描页,方便现场用户扫描关注微信公众账号。 由于场地...

网友评论

      本文标题:点击按钮扫描二维码

      本文链接:https://www.haomeiwen.com/subject/puxukqtx.html