如今在国内无论走到哪里,只要是涉及支付的方面都可以使用支付宝或微信扫个二维码进行付款。现在二维码随处可见,二维码支付,二维码点餐,二维码坐公交,二维码做地铁,广告宣传页上, 电视台节目上等等。另外连乞丐都用上二维码了,你没带钱没关系,扫码给乞丐。二维码公交,地铁,点餐,支付已经是一种潮流,我们必须顺应潮流。IOS怎么实现扫描二维码功能呢?

直接复制代码粘贴一下即可使用,贴上@2x @3x图片,另外权限别忘了

//
//  QRViewController.m
//  TYSmartTimerKit
//
//  Created by Eifi on 2019/7/3.
//  Copyright © 2019 sz_bofu. All rights reserved.
//#import "QRViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface QRViewController ()<AVCaptureMetadataOutputObjectsDelegate, AVCaptureVideoDataOutputSampleBufferDelegate>{NSTimer *_timer;BOOL upOrDown;int num;
}
@property (strong, nonatomic) AVCaptureDevice * device;
@property (strong, nonatomic) AVCaptureDeviceInput * input;
@property (strong, nonatomic) AVCaptureMetadataOutput * output;
@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer * preview;
@property (strong, nonatomic) UIImageView *imageView;
@property (retain, nonatomic) UIImageView *line;
@property (nonatomic, strong) NSString     *identifier;
@end@implementation QRViewController
- (AVCaptureDevice *)device{if (_device == nil) {_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];}return _device;
}- (AVCaptureDeviceInput *)input{if (_input == nil) {_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];}return _input;
}- (AVCaptureMetadataOutput *)output{if (_output == nil) {_output = [[AVCaptureMetadataOutput alloc]init];[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];//限制扫描区域(上下左右)[_output setRectOfInterest:[self rectOfInterestByScanViewRect:_imageView.frame]];}return _output;
}- (CGRect)rectOfInterestByScanViewRect:(CGRect)rect {CGFloat width = CGRectGetWidth(self.view.frame);CGFloat height = CGRectGetHeight(self.view.frame);CGFloat x = (height - CGRectGetHeight(rect)) / 2 / height;CGFloat y = (width - CGRectGetWidth(rect)) / 2 / width;CGFloat w = CGRectGetHeight(rect) / height;CGFloat h = CGRectGetWidth(rect) / width;return CGRectMake(x, y, w, h);
}- (AVCaptureSession *)session{if (_session == nil) {//session_session = [[AVCaptureSession alloc]init];[_session setSessionPreset:AVCaptureSessionPresetHigh];if ([_session canAddInput:self.input]) {[_session addInput:self.input];}if ([_session canAddOutput:self.output]) {[_session addOutput:self.output];}}return _session;
}- (AVCaptureVideoPreviewLayer *)preview{if (_preview == nil) {_preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];}return _preview;
}
- (void)viewDidLoad {[super viewDidLoad];}
- (void)viewWillAppear:(BOOL)animated{[super viewWillAppear:animated];if (![TYUtils isEnableCamera]) {[UIAlertView bk_showAlertViewWithTitle:NSLocalizedString(@"", @"") message:NSLocalizedString(@"请在iphone的“设置-隐私-相机”选项中,允许定时宝访问你的相机", @"") cancelButtonTitle:NSLocalizedString(@"取消", @"") otherButtonTitles:@[NSLocalizedString(@"前往", @"")] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {if (buttonIndex == 0) {[tp_topMostViewController().navigationController popViewControllerAnimated:YES];}else {NSURL *prefs = [TYUtils prefsUrlWithQuery:@{@"root":@"General"}];[[UIApplication sharedApplication] openURL:prefs];}}];}else {[self initView];}}- (void)initView {if (self.device == nil) {[UIAlertView bk_showAlertViewWithTitle:NSLocalizedString(@"未检测到摄像头!", @"") message:@"" cancelButtonTitle:NSLocalizedString(@"确定", @"") otherButtonTitles:@[] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {[self.navigationController popViewControllerAnimated:YES];}];return;}[self addTimer];[self addImageView];[self scanSetup];}//添加扫描框
- (void)addImageView{_imageView = [[UIImageView alloc]initWithFrame:CGRectMake((APP_SCREEN_WIDTH-(self.view.centerX+30))/2, (APP_SCREEN_HEIGHT-(self.view.centerX + 30))/2, self.view.centerX + 30, self.view.centerX + 30)];//显示扫描框_imageView.image = [UIImage imageNamed:@"ty_qrcode_bg"];[self.view addSubview:_imageView];_line = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_imageView.frame)+5, CGRectGetMinY(_imageView.frame)+5, CGRectGetWidth(_imageView.frame), 3)];_line.image = [UIImage imageNamed:@"ty_qrcode_line"];[self.view addSubview:_line];UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];[lable setText:@"将二维码放入框内,自动识别"];lable.textAlignment = NSTextAlignmentCenter;lable.textColor = [UIColor whiteColor];lable.font = [UIFont systemFontOfSize:14];lable.center = CGPointMake(_imageView.center.x , _imageView.center.y+ (self.view.centerX + 30)/2 + 30);[self.view addSubview:lable];
}//初始化扫描配置
- (void)scanSetup{self.preview.frame = self.view.bounds;self.preview.videoGravity = AVLayerVideoGravityResize;[self.view.layer insertSublayer:self.preview atIndex:0];[self.output setMetadataObjectTypes:@[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeQRCode]];[self.session setSessionPreset:AVCaptureSessionPresetHigh];[self.session startRunning];}- (void)viewDidDisappear:(BOOL)animated {[self.session stopRunning];[_timer setFireDate:[NSDate distantFuture]];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {if ([metadataObjects count] > 0) {AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];if ([metadataObject isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) {NSString *stringValue = [metadataObject stringValue];if (stringValue != nil) {[self.session stopRunning];NSLog(@"%@",stringValue);}}}
}- (void)addTimer{_timer = [NSTimer scheduledTimerWithTimeInterval:0.008 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}
//控制扫描线上下滚动
- (void)timerMethod{if (upOrDown == NO) {num ++;_line.frame = CGRectMake(CGRectGetMinX(_imageView.frame)+5, CGRectGetMinY(_imageView.frame)+5+num, CGRectGetWidth(_imageView.frame)-10, 3);if (num == (int)(CGRectGetHeight(_imageView.frame)-10)) {upOrDown = YES;}}else{num --;_line.frame = CGRectMake(CGRectGetMinX(_imageView.frame)+5, CGRectGetMinY(_imageView.frame)+5+num, CGRectGetWidth(_imageView.frame)-10, 3);if (num == 0) {upOrDown = NO;}}
}
//暂定扫描
- (void)stopScan{[self.session stopRunning];[_timer setFireDate:[NSDate distantFuture]];_line.hidden = YES;
}
- (void)starScan{[self.session startRunning];[_timer setFireDate:[NSDate distantPast]];_line.hidden = NO;
}@end

IOS实现扫描二维码,利用系统API实现,看了这篇IOS扫描功能再也不用求人了。相关推荐

  1. 实现手机扫描二维码页面登录,类似web微信-第一篇,业务分析

    关于XMPP组件的文章,先休息两天,好歹已经完整的写了一份. 这两天,先实现一套关于web微信扫描二维码页面登录的试验,因为这种模式在我们的很多业务场景里大有前途. 首先介绍一下web微信登录的过程 ...

  2. 二维码如何转为链接,看了这篇文章才知道多好用!

    第一步 打开浏览器,在地址栏中输入链接:https://sm.ms/ 第二步 选择如下图中的 Select Image(s) 进行上传图片: 第三步 上传完后,会显示图片,然后点击Upload选项即可 ...

  3. h5端呼起摄像头扫描二维码并解析

    2016年6月29日补充: 最近做了一些与表单相关的项目,使用了h5的input控件,在使用过程中遇到了很多的坑.也包括与这篇文章相关的. 首先我们应该知道使用h5新提供的属性getUserMedia ...

  4. (4.3.1.11)微信扫描二维码无法下载apk文件解决办法

    最近客户网站扫描二维码下载apk的功能不能用了,客户反应扫描二维码后是一片空白,本以为是文件丢失,可是服务器上面文件位置都是正常的,后来才发现是因为腾讯屏蔽掉了外部文件的链接,经过几次不断的尝试,发现 ...

  5. Unity之生成扫描二维码

    Unity之生成扫描二维码 Unity之生成扫描二维码 前言 开篇 Unity版本及使用插件 正题 前期准备 首先生成二维码 然后需要扫描二维码 该使用了 挂载脚本绑定按钮和输入框 运行内容 生成二维 ...

  6. 标签软件如何实现扫描二维码显示汉字

    在众多的标签打印软件中,制作带有文字.数字.条形码等内容的标签是最常见的,那么对于二维码该如何制作或者如果给二维码添加内容呢?而我们扫描二维码有时候显示的是普通的几个汉字,有时候是一个网页页面,那么这 ...

  7. iOS原生实现二维码扫描

    iOS原生实现二维码扫描 最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时 ...

  8. 扫描二维码下载app,判断是Android还是ios,并跳转到不同的下载地址

    写在前面 通常,我们开发一个app,有Android版本,有ios版本,但是,我们有时候只有一个二维码可以放置,比如,通过社会化分享,分享到微信一张图片,通过微信识别二维码来下载我们的app,此时,只 ...

  9. iOS中 扫描二维码/生成二维码详解

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 指示根视图: ? 1 self.window.rootViewController = [[UINav ...

最新文章

  1. Reddit年度盘点:2019年最佳机器学习项目
  2. 使用ASP.NET Atlas开发随输入内容自动调整行数的textarea(转)
  3. 算法与数据结构(约瑟夫问题)
  4. canopy算法 java_mahout源码canopy算法分析之二CanopyMapper
  5. AnsroidStudio中提示:Failed to find configured root that contain
  6. NCspider项目总结
  7. 《黑书》hdu1271递推习题:整数对
  8. P1110 [ZJOI2007]报表统计
  9. CuteEditor 5.0 的使用
  10. cmake 简介-初识
  11. 大小写 字符串_C# 读取环境变量,和字符串大小写转换
  12. 如何在Python中将元素添加到列表
  13. 《图论及其应用》学习笔记(图和简单图)
  14. Arduino学习笔记 TB6612FNG模块
  15. 平时杂技,串口摄像头+portmon.exe
  16. Mercury老版路由器WDS
  17. 富康路小学毕业郭佳华
  18. 通过2-3-4树理解红黑树
  19. 2015人大计算机复试线,清华、北大、人大、公布2015年考研复试线
  20. Web 智能代码编辑器 WeBuilder 2022

热门文章

  1. 中国改性淀粉市场深度调研报告(2022版)
  2. 变质量力学问题与虚功原理解答
  3. [ICCV2019]DF2Net: A Dense-Fine-Finer Network for Detailed 3D Face Reconstruction
  4. Lerna入门与实战
  5. ORB-SLAM3 土味分析
  6. 苏涛:对抗样本技术在互联网安全领域的应用
  7. SAS中删除数据集学习
  8. 【计算机体系结构】什么是流水线?
  9. aide可以用mysql吗,aide连接mysql
  10. Unity Animation设置动画播放初始时间