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:// ...
随机推荐
- 全志A33平台编译linux(分色排版)sina33
全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...
- 关于Python多线程condition变量的应用
''' 所谓条件变量,即这种机制是在满足了特定的条件后,线程才可以访问相关的数据. 它使用Condition类来完成,由于它也可以像锁机制那样用,所以它也有acquire方法和release方法,而且 ...
- Fiddler——抓包工具的使用
fiddler安装 pc端安装fiddler,自行从百度下载即可 Fiddler是强大且好用的Web调试工具之一,它能记录客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输 ...
- 【sqli-labs】 less61 GET -Challenge -Double Query -5 queries allowed -Variation4 (GET型 挑战 双查询 只允许5次查询 变化4)
http://192.168.136.128/sqli-labs-master/Less-61/?id=1' 单引号双括号闭合 192.168.136.128/sqli-labs-master/Les ...
- Windows提高_2.2第二部分:用户区同步
第二部分:用户区同步 同步和互斥 同步:就是按照一定的顺序执行不同的线程 互斥:当一个线程访问某一资源的时候,其它线程不能同时访问 多线程产生的问题 #include <stdio.h> ...
- C++ map使用总结
0. Backgroud 此文章源于博主(sunshinewave),转到自己博客以后方便查看 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次, ...
- 洛谷——P1516 青蛙的约会
P1516 青蛙的约会 题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件 ...
- python求两个链表组成的数字的和
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- pageContext对象的使用及常用方法
pageContext对象的使用及常用方法 制作人:全心全意 获取页面上下文的pageContext对象是一个比较特殊的对象,通过它可以获取JSP页面的request.response.session ...
- 【模板】51nod 1006 最长公共子序列Lcs
[题解] dp转移的时候记录一下,然后倒着推出答案即可. #include<cstdio> #include<cstring> #include<algorithm> ...