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:// ...
随机推荐
- 中国移动Lumia机强制升级Windows10手机开发者预览版的方法
[最新消息4-9]微软已经确定将于PST太平洋标准时间周五上午十点也就是北京时间本周六(4-11)凌晨一点推送更新Windows10手机预览版 但是推送更新的机型不包括Lumia Icon 和930 ...
- 微信小程序资料总结
http://blog.csdn.net/ZCLengendary/article/details/54312030 --添加html标签与处理 https://www.cnblogs.com/HD ...
- python自动化--接口请求及封装
基于http协议,最常用的是GET和POST两种方法. 接口文档需要包含哪些信息: 接口名称接口功能接口地址支持格式 json/xml请求方式请求示例请求参数(是否必填.数据类型.传递参数格式)返回参 ...
- Angular——依赖注入
基本介绍 1.AngularJS采用模块化的方式组织代码,将一些通用逻辑封装成一个对象或函数,实现最大程度的复用,这导致了使用者和被使用者之间存在依赖关系. 2.所谓依赖注入是指在运行时自动查找依赖关 ...
- mysqlworkbench 执行update语句报错:You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY colum ...
- java 23种设计模式 链接集锦
创建型抽象工厂模式 http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html工厂方法 http://www.cnblogs ...
- 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...
- 15Microsoft SQL Server 数据库维护
Microsoft SQL Server 数据库维护 2.6.1数据库联机与脱机 --联机:该状态为数据库正常状态,也就是我们常看到的数据库的状态,该状态下的数据库处于可操作状态,可以对数据库进行任何 ...
- 让元素div消失在视野中
让元素div消失在视野中1.position:absolute/relative/fixed + 方位 top/bottom/left/right: -9999px2.display:none3.vi ...
- 支持向量机(SVM)原理浅析
因为网页博客输入公式很麻烦,所以就在word上面写了,然后截图发上来. 后续关于SVM和FC在深度学习当中得使用对比分析,我再补充.