iOS源生二维码扫描,你想象不到的强大
2015-04-11 14:22
357 查看
还在用ZBar、ZXing等第三方库来实现二维码扫描吗?那就OUT了。来看下iOS源生的二维码扫描,【测试需真机】
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>//需要添加AVFoundation系统库
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//遵守相应代理
@property (strong,nonatomic) AVCaptureDevice * device;//采集的设备
@property (strong,nonatomic) AVCaptureDeviceInput * input;//设备的输入
@property (strong,nonatomic) AVCaptureMetadataOutput * output;//输出
@property (strong,nonatomic) AVCaptureSession * session;//采集流
@property (strong,nonatomic) AVCaptureVideoPreviewLayer * preview;//窗口
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"iOS原生扫描二维码" forState:UIControlStateNormal];
button.frame = CGRectMake(50, 100, self.view.bounds.size.width - 100, 50);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)buttonClick{
// Device 实例化设备
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input 设备输入
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output 设备的输出
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
_session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
_output.metadataObjectTypes =@[AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeITF14Code,AVMetadataObjectTypeDataMatrixCode];
// Preview 扫描窗口设置
_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame = CGRectMake(20,150,self.view.bounds.size.width - 40,300);
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start 开始扫描
[_session startRunning];
}
//解析结果的代理方法
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//得到解析到的结果
NSString *stringValue;
if ([metadataObjects count] > 0)
{
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
}
[_session stopRunning];//停止扫描
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"结果:%@",stringValue] delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了",@"重新扫描", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self dismissViewControllerAnimated:YES completion:^
{
//
}];
}
else
{
//重新扫描
[_session startRunning];
}
}
@end
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>//需要添加AVFoundation系统库
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//遵守相应代理
@property (strong,nonatomic) AVCaptureDevice * device;//采集的设备
@property (strong,nonatomic) AVCaptureDeviceInput * input;//设备的输入
@property (strong,nonatomic) AVCaptureMetadataOutput * output;//输出
@property (strong,nonatomic) AVCaptureSession * session;//采集流
@property (strong,nonatomic) AVCaptureVideoPreviewLayer * preview;//窗口
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"iOS原生扫描二维码" forState:UIControlStateNormal];
button.frame = CGRectMake(50, 100, self.view.bounds.size.width - 100, 50);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)buttonClick{
// Device 实例化设备
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input 设备输入
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output 设备的输出
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
_session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
_output.metadataObjectTypes =@[AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeITF14Code,AVMetadataObjectTypeDataMatrixCode];
// Preview 扫描窗口设置
_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame = CGRectMake(20,150,self.view.bounds.size.width - 40,300);
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start 开始扫描
[_session startRunning];
}
//解析结果的代理方法
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//得到解析到的结果
NSString *stringValue;
if ([metadataObjects count] > 0)
{
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
}
[_session stopRunning];//停止扫描
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"结果:%@",stringValue] delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了",@"重新扫描", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self dismissViewControllerAnimated:YES completion:^
{
//
}];
}
else
{
//重新扫描
[_session startRunning];
}
}
@end
相关文章推荐
- 【木头Cocos2d-x 025】状态机篇(第04章) --事件驱动,你想象不到的强大
- 【木头Cocos2d-x 025】状态机篇(第04章) --事件驱动,你想象不到的强大
- 一般人想象不到的创业者付出的5种努力 创业者的背后
- Google服务器机组强大超出你想象(图)
- 比你想象中还要强大的枚举类型---案例篇
- 你想象不到的SQL语句?
- 同一片蓝天下,有些人以你想象不到的方式活着
- Web应用:当文件超过100KB,无法上传,有种原因你想象不到
- BUG出现的地方真的令我这个测试新人想象不到
- 【想象不到的俄罗斯】……太震撼了……
- java细节,细的你想象不到
- SQL CASE的用法,比想象中的强大
- 并非如你想象的那般强大,带你重新认识3D打印
- 想象不到的网站注入点
- 比你想象中还要强大的枚举类型
- 比你想象中还要强大的枚举类型
- sql语句经典,凡是做开发的,用不到这里面绝对不可能!强大
- AutoIt:AutoIt比我想象的更加强大
- JS代码模仿键盘功能,只有不到10K太强大了
- 同一片蓝天下,有些人以你想象不到的方式活着