ios7新增api实现扫描二维码
本来用的ZBar开源库实现的扫描二维码,可是貌似不支持arm64了,也没有在更新。
如今不用适配ios7下面。而iOS新增系统API已支持扫码,參考老外的一篇博客做了个demo。须要的能够參考下
參考博客:http://www.appcoda.com/qr-code-ios-programming-tutorial/
#import <AVFoundation/AVFoundation.h>
@interface QRCodeReadController : BaseViewController<AVCaptureMetadataOutputObjectsDelegate> @property (weak, nonatomic) IBOutlet UIView *viewPreview;
@end
在xib上加一个viewPreview,用来扫码时动态显示获取到的摄像头的内容
@interface QRCodeReadController ()
{
NSInteger maxY;
NSInteger minY;
NSTimer *timer; UIImageView *line;
}
@property (nonatomic) BOOL isReading; @property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer; -(BOOL)startReading;
-(void)stopReading; @end @implementation QRCodeReadController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_isReading = NO;
if ([self startReading]) {
maxY = 280;
minY = 2;
line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 10)]; // 0 -200
[line setImage:[UIImage imageNamed:@"e0"]];
[_viewPreview addSubview:line]; timer = [NSTimer scheduledTimerWithTimeInterval:1.0/40 target:self selector:@selector(move) userInfo:nil repeats:YES];
}; } /*
*
*
AVCaptureMetadataOutput object. This class in combination with the AVCaptureMetadataOutputObjectsDelegate protocol will manage to intercept any metadata found in the input device (meaning data in a QR code captured by our camera) and translate it to a human readable format.
*/
- (BOOL)startReading {
NSError *error; AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}
_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input]; AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput]; dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]]; //show to user what the camera of the device sees using a AVCaptureVideoPreviewLayer
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
[_viewPreview.layer addSublayer:_videoPreviewLayer]; [_captureSession startRunning]; return YES;
} -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
NSLog(@"metadataObj string = %@",[metadataObj stringValue]);
_isReading = NO;
}
}
} -(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil; [_videoPreviewLayer removeFromSuperlayer];
} // 扫描时,移动扫描线
-(void)move
{
NSLog(@"+++");
static BOOL flag = TRUE; // true down and false up
if (flag) {
if (line.frame.origin.y <maxY) {
line.frame = CGRectMake(
line.frame.origin.x, line.frame.origin.y +5,
line.frame.size.width, line.frame.size.height
);
}else{
flag = !flag;
if (_isReading&&[timer isValid]) {
[timer invalidate];
}
}
}else
{
if (line.frame.origin.y >minY) {
line.frame = CGRectMake(
line.frame.origin.x, line.frame.origin.y -5,
line.frame.size.width, line.frame.size.height
);
}else
{
flag = !flag;
}
} }
/*****************************************************************************************/
识别图片二维码
如今有从含二维码的图片直接出二维码中信息的需求,查相关资料发现。原生api在iOS8才支持(CIQRCodeFeature
)
见 http://stackoverflow.com/questions/27505420/is-it-possible-to-decode-qrcode-image-to-value
解决方式用的zxing的ZXQRcodeDecoder
http://stackoverflow.com/questions/15575554/zxingobjc-cant-decode-image-taken-from-uiimagepickercontroller
NSString *path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path]; NSError *error = nil; ZXQRCodeReader *reader = [[ZXQRCodeReader alloc]init]; ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:[image CGImage]];
ZXHybridBinarizer *binazer = [ZXHybridBinarizer binarizerWithSource:source];
ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc]initWithBinarizer:binazer]; ZXResult *result = [reader decode:bitmap
hints:nil
error:&error];
if(result){
NSLog(@"%@",result);
[[[UIAlertView alloc] initWithTitle:@"Success" message:result.text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
} else {
// Use error to determine why we didn't get a result, such as a barcode
// not being found, an invalid checksum, or a format inconsistency.
[[[UIAlertView alloc] initWithTitle:@"ERROR" message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
}
ios7新增api实现扫描二维码的更多相关文章
- Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)
二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...
- 对于ios7扫描二维码功能的实现
在ios7曾经,我们开发二维码扫描,或者生产都须要借助第三方的开源库进行开发. 然后升级到ios7时,在passbook中苹果自带二维码扫描功能,并且扫描速度很快,秒杀一切第三方开源库. 所以,我们做 ...
- Android进阶笔记06:Android 实现扫描二维码实现网页登录
一. 扫描二维码登录的实现机制: 详细流程图: (1)PC端打开网页(显示出二维码),这时候会保存对应的randnumber(比如:12345678). (2)Android客户端扫码登录,Andro ...
- iOS中 扫描二维码/生成二维码详解 韩俊强的博客
最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客
近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- 用c#开发微信 (20) 微信登录网站 - 扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 1 创建网站应用 在微信开放平台创建一个网站应用 https:// ...
- JAVA实现的微信扫描二维码支付
吐槽一下 支付项目采用springMvc+Dubbo架构实现,只对外提供接口. 话说,为什么微信支付比支付宝来的晚了那么一点,一句话,那一阵挺忙的,然后就没有时间整理,最近做完支付宝支付,顺便也把微信 ...
- Asp.Net微信登录-电脑版扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...
- C#微信登录-电脑版扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...
随机推荐
- [ HDOJ 3826 ] Squarefree number
\(\\\) \(Description\) \(T\)组数据,每次给出一个正整数 \(N\) ,判断其是否能被任意一个完全平方数整除. \(T\le 20,N\le 10^{18}\) \(\\\) ...
- (2)左右值初探与auto类型说明符
这篇文章的起因是下面这两段代码,出自<C++ primer 5th>中文版P62页: auto &h =42;//错误,不能为非常量引用绑定字面值 const auto & ...
- LR接口测试---Java Vuser之增删改查
import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...
- PKI中常用编码:ASN.1 DER BER Base64
迟到了两年的笔记... 在PKI的应用中,常会用到以下几个编码概念: ASN.1(Abstract Syntax Notation One, 抽象语法标记) 定义:A standard interfa ...
- 如何利用CSS中的ime-mode用来控制页面上文本框中的全角/半角输入
css 之 ime-mode语法:ime-mode : auto | active | inactive | disabled取值:auto : 默认值.不影响ime的状态.与不指定 ime-mode ...
- 云服务IaaS,PaaS,SaaS
IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a-service SaaS:软件服务,Software-as-a-serv ...
- vue中的input使用e.target.value赋值的问题
很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...
- Linux系统安装,组成及开关机
Linux系统安装,组成及开关机 系统安装 swap分区用于实现虚拟内存,文件系统类型是swap. /分区用于存放包括系统程序和用户数据在内的所有数据,文件系统类型是ext4. 系统组成 Linux内 ...
- 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)
Dijkstra+ 链式前向星+ 优先队列 Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...
- <MySQL>入门六 变量
/* 变量 系统变量: 全局变量 会话变量 自定义变量 用户变量 局部变量 */ -- ------------系统变量-------------------- /* 变量由系统提供,不是用户定义,属 ...