iOS二维码扫描

测试环境:iPhone 5s  iOS7.1

首先添加框架AVFoundation.framework,然后导入自定义类CMCaptureViewController.h、CMCaptureViewController.m,以及用到的图片资源:[email protected]、[email protected]。

用的时候只需要切换到二维码视图控制器CMCaptureViewController即可,会跳到二维码扫描界面

如:

1 [self presentViewController:captureViewController animated:YES completion:nil];

要获得扫描的二维码内容,只需要成为其委托,实现委托方法

1 - (void)captureController:(CMCaptureViewController *)controller data:(id)metadata;

参数metadata中包含扫描到的字符串。

CMCaptureViewController.h、CMCaptureViewController.m的源代码如下:

 1 //  CMCaptureViewController.h
 2 //  二维码扫描
 3
 4 #import <UIKit/UIKit.h>
 5 #import <AVFoundation/AVFoundation.h>
 6
 7 @class CMCaptureViewController;
 8 @protocol CMCaptureViewDelegate <NSObject>
 9
10 - (void)captureController:(CMCaptureViewController *)controller data:(id)metadata;
11
12 @end
13
14 @interface CMCaptureViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
15 {
16     int num;
17     BOOL upOrdown;
18     NSTimer * timer;
19 }
20 @property (strong,nonatomic)AVCaptureDevice * device;
21 @property (strong,nonatomic)AVCaptureDeviceInput * input;
22 @property (strong,nonatomic)AVCaptureMetadataOutput * output;
23 @property (strong,nonatomic)AVCaptureSession * session;
24 @property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
25 @property (nonatomic, retain) UIImageView * line;
26 @property (nonatomic, weak) id <CMCaptureViewDelegate> delegate;
27
28 @end
  1 //  CMCaptureViewController.m
  2 //  二维码扫描
  3
  4
  5 #import "CMCaptureViewController.h"
  6
  7 @interface CMCaptureViewController ()
  8
  9 @end
 10
 11 @implementation CMCaptureViewController
 12
 13 - (void)loadView
 14 {
 15     [super loadView];
 16     self.view.backgroundColor = [UIColor redColor];
 17     UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 18     [scanButton setTitle:@"取消" forState:UIControlStateNormal];
 19     scanButton.frame = CGRectMake(100, 420, 120, 40);
 20     [scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
 21     [self.view addSubview:scanButton];
 22
 23     UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 40, 290, 50)];
 24     labIntroudction.backgroundColor = [UIColor clearColor];
 25     labIntroudction.numberOfLines=2;
 26     labIntroudction.textColor=[UIColor whiteColor];
 27     labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
 28     [self.view addSubview:labIntroudction];
 29
 30
 31     UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 300, 300)];
 32     imageView.image = [UIImage imageNamed:@"pick_bg"];
 33     [self.view addSubview:imageView];
 34
 35     upOrdown = NO;
 36     num =0;
 37     _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 110, 220, 2)];
 38     _line.image = [UIImage imageNamed:@"line.png"];
 39     [self.view addSubview:_line];
 40
 41     timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
 42
 43 }
 44
 45
 46 -(void)animation1
 47 {
 48     if (upOrdown == NO) {
 49         num ++;
 50         _line.frame = CGRectMake(50, 110+2*num, 220, 2);
 51         if (2*num == 280) {
 52             upOrdown = YES;
 53         }
 54     }
 55     else {
 56         num --;
 57         _line.frame = CGRectMake(50, 110+2*num, 220, 2);
 58         if (num == 0) {
 59             upOrdown = NO;
 60         }
 61     }
 62
 63 }
 64
 65 -(void)backAction
 66 {
 67
 68     [self dismissViewControllerAnimated:YES completion:^{
 69         [timer invalidate];
 70     }];
 71 }
 72
 73
 74
 75 -(void)viewWillAppear:(BOOL)animated
 76 {
 77     [self setupCamera];
 78 }
 79 - (void)setupCamera
 80 {
 81     // Device
 82     _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
 83
 84     // Input
 85     _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
 86
 87     // Output
 88     _output = [[AVCaptureMetadataOutput alloc]init];
 89     [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
 90
 91     // Session
 92     _session = [[AVCaptureSession alloc]init];
 93     [_session setSessionPreset:AVCaptureSessionPresetHigh];
 94     if ([_session canAddInput:self.input])
 95     {
 96         [_session addInput:self.input];
 97     }
 98
 99     if ([_session canAddOutput:self.output])
100     {
101         [_session addOutput:self.output];
102     }
103
104     // 条码类型 AVMetadataObjectTypeQRCode
105     _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
106
107     // Preview
108     _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
109     _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
110     _preview.frame =CGRectMake(20,110,280,280);
111     [self.view.layer insertSublayer:self.preview atIndex:0];
112
113     // Start
114     [_session startRunning];
115 }
116 #pragma mark AVCaptureMetadataOutputObjectsDelegate
117 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
118 {
119
120     NSString *stringValue;
121
122     if ([metadataObjects count] >0)
123     {
124         AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
125         stringValue = metadataObject.stringValue;
126         NSLog(@"[%@]", stringValue);
127     }
128
129     [_session stopRunning];
130
131     [self dismissViewControllerAnimated:YES completion:^
132     {
133         [timer invalidate];
134
135         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
136             if ([self.delegate respondsToSelector:@selector(captureController:data:)])
137             {
138                 [self.delegate captureController:self data:stringValue];
139             }
140         });
141
142     }];
143 }
144
145 - (void)didReceiveMemoryWarning
146 {
147     [super didReceiveMemoryWarning];
148     // Dispose of any resources that can be recreated.
149 }
150
151
152
153 @end

[email protected]

[email protected]

时间: 2024-08-01 22:33:33

iOS二维码扫描的相关文章

Ios二维码扫描(系统自带的二维码扫描)

Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你当前的控制器中导入 #import <AVFoundation/AVFoundation.h> <AVCaptureMetadataOutputObjectsDelegate>代理 3).在@interface中定义 @property (nonatomic,strong)AVCapt

IOS二维码扫描IOS7系统实现

扫描相关类 二维码扫描需要获取摄像头并读取照片信息,因此我们需要导入系统的AVFoundation框架,创建视频会话.我们需要用到一下几个类: AVCaptureSession 会话对象.此类作为硬件设备输入输出信息的桥梁,承担实时获取设备数据的责任 AVCaptureDeviceInput 设备输入类.这个类用来表示输入数据的硬件设备,配置抽象设备的port AVCaptureMetadataOutput 输出类.这个支持二维码.条形码等图像数据的识别 AVCaptureVideoPrevie

iOS 二维码扫描报错问题之一

iOS 开发中在导入第三方的二维码扫描 ZBarSDK在模拟器能正常运行,但真机调试的过程中可能会报错,报错的信息如下目前的找到的一种解决办法如下图所示:在Build Settings里面搜bit  在BuildOptions里面修改Enable Bitcode为No,这样真机就可以正常运行了,如下图所示: 修改之后目前真机上可以正常运行

ios 二维码扫描

https://github.com/bmorton/ZBarSDK 我是用ZBar做的二维码扫描 其他的SDK也可以 首先是导入库文件: 1.AVFoundation.framework 2.CoreMedia.framework 3.CoreVideo.framework 4.QuartzCore.framework 5.libiconv.dylib 这个5个库文件 在ViewController.h 导入#import "ZBarSDK.h" 并且 继承 <ZBarRead

iOS二维码扫描的实现(Swift)

随着二维码的普遍使用,二维码扫描也成为了很多app的一个基本功能,本篇主要来介绍一下如何实现一个简单的二维码扫描功能.本文使用了XCode自带的AVFoundation 库,利用Swfit语言实现. 实现的步骤如下: 1.获取视频设备(Video) 在二维码扫描中,我们的输入流是视频.我们需要enable视频设备来获取相应的元数据. 2. 创建Session来处理视频的输入输出流 3. 创建输入输出流,并添加至Session中 4. 处理二维码数据 该方法是AVCaptureMetadataOu

iOS二维码扫描,你需要注意的两件事

在 iOS7 以前,在iOS中实现二维码和条形码扫描,我们所知的有,两大开源组件ZBar与ZXing. 这两大组件我们都有用过,这里总结下各自的缺点: ZBar ZBar在扫描的灵敏度上,和内存的使用上相对于ZXing上都是较优的,但是对于 “圆角二维码” 的扫描确很困难.如: ZXing ZXing 是 Google Code上的一个开源的条形码扫描库,是用java设计的,连Google Glass 都在使用的.但有人为了追求更高效率以及可移植性,出现了c++ port. Github上的Ob

iOS 二维码扫描 zBarsdk 不支持64位 missing required architecture x86_64 in file

闲话少讲,实际上ios7以上是支持原生api扫描二维码的,所以我觉得这就是为什么zbarsdk没有继续更新的原因. 百度一下,说修改架构什么的还是有的,但貌似没说到点子上,还是老外的解决方法牛逼. 第一:问题的提出,给了很多解决方法: http://stackoverflow.com/questions/12506671/zbar-sdk-is-not-working-in-ios6/12753812#12753812 stackoverflow的方法还是不错的. 这个也不错:http://ww

iOS AV Foundation 二维码扫描 02 扫码

AVFoundation支持以下一维和二维码的扫描: QR code Aztec EAN13 EAN8 UPC-E PDF417 Code 93 Code 39 Code 39 mode 41 QR code就是我们所熟知的二维码. 打开ViewController.m,添加以下实例变量: AVCaptureMetadataOutput *_metadataOutput; 当从视频帧中检测到元数据时,AVCaptureMetadataOutput会调用应用程序的回调函数.AV Foundatio

iOS开发-二维码扫描和应用跳转

iOS开发-二维码扫描和应用跳转 序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦,出错也不方便调试.在iOS7之后,苹果自身提供了二维码的扫描功能,从效率上来说,原生的二维码远高于这些第三方框架.本文讲解如何使用原生框架实现二维码扫描功能,并且进行扫描后的项目跳转.ps:本期的源代码会在文章结尾给出链接 扫描相关类 二维码扫描需要获取摄像头并读取