本来用的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实现扫描二维码的更多相关文章

  1. Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)

    二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...

  2. 对于ios7扫描二维码功能的实现

    在ios7曾经,我们开发二维码扫描,或者生产都须要借助第三方的开源库进行开发. 然后升级到ios7时,在passbook中苹果自带二维码扫描功能,并且扫描速度很快,秒杀一切第三方开源库. 所以,我们做 ...

  3. Android进阶笔记06:Android 实现扫描二维码实现网页登录

    一. 扫描二维码登录的实现机制: 详细流程图: (1)PC端打开网页(显示出二维码),这时候会保存对应的randnumber(比如:12345678). (2)Android客户端扫码登录,Andro ...

  4. iOS中 扫描二维码/生成二维码详解 韩俊强的博客

    最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  5. iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客

    近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  6. 用c#开发微信 (20) 微信登录网站 - 扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 1 创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  7. JAVA实现的微信扫描二维码支付

    吐槽一下 支付项目采用springMvc+Dubbo架构实现,只对外提供接口. 话说,为什么微信支付比支付宝来的晚了那么一点,一句话,那一阵挺忙的,然后就没有时间整理,最近做完支付宝支付,顺便也把微信 ...

  8. Asp.Net微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  9. C#微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

随机推荐

  1. 42使用NanoPiM1Plus在Android4.4.2下的录音测试

    42使用NanoPiM1Plus在Android4.4.2下的录音测试 大文实验室/大文哥壹捌陆捌零陆捌捌陆捌贰21504965 AT qq.com完成时间:2017/12/5 17:51版本:V1. ...

  2. ubantu MongoDB安装

    转 https://blog.csdn.net/flyfish111222/article/details/51886787 本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的 ...

  3. Protocol(协议)

    Protocol(协议) (一) (1)简介 1.Protocol:就一个用途,用来声明一大堆的方法(不能声明成员变量),不能写实现.看起来类似于一个类的接口, 不同的是协议没有父类,也不能定义实例变 ...

  4. java操作zip文件

    思路: 1).读取zip中的文件并将除了重名文件之外的文件转存到中转zip文件中. 2).往中转文件中插入txt文件. 3).删除原zip文件. 4).将中转zip文件重命名为原zip文件. 前提,t ...

  5. jsTree插件简介(三)

    UI-plugin JSTree的UI插件:用来处理选择.不选和鼠标悬浮树选项的插件. 一.属性包括: 1.select_limit:指定一次可以选中几个节点,默认为-1,表示无限制选中. 2.sel ...

  6. URL解析-URLComponents

    let components = URLComponents(url: fakeUrl, resolvingAgainstBaseURL: false)! http://10.100.140.84/m ...

  7. Rest 参数(...)

    javascript 之Rest 参数(...) ES6 Rest参数 Rest就是为解决传入的参数数量不一定, rest parameter(Rest 参数) 本身就是数组,数组的相关的方法都可以用 ...

  8. JAVA基础数组

    数组: 数组是一种容器 存储同一类型的一组数据(必须是 类型相同的一组数据) 定义数组的公式:(有两种) 1.静态定义      1)数据类型[ ] 数组名 = {元素1,元素2,元素3,元素4,元素 ...

  9. C/C++野指针

    野指针: 野指针不同于空指针,空指针是指一个指针的值为null,而野指针的值并不为null,野指针会指向一段实际的内存,只是它指向哪里我们并不知情,或者是它所指向的内存空间已经被释放,所以在实际使用的 ...

  10. 骑士游历 - dp

    题目地址:http://www.51cpc.com/web/problem.php?id=1586 Summarize: 1. 题目坐标系所给 x,y与惯用表示横纵坐标相反 2. 搜索超时,使用动规: ...