iOS 开发每天分享优质文章iOS开发

iOS二维码的直接扫描的读取

2016-12-13  本文已影响90人  邵琼

分享的内容,大家看了后,不管有什么问题或者建议,都可以说出来,我都会一一做答,一起加油啦

我的下一篇 <<iOS从相册读取二维码>>需要的话也可以借鉴,原理一样

我的上一篇<<iOS 二维码的生成>>也可以学习

一般公司业务中,二维码的生成与获取是同时进行的.比如我们公司做智能门锁的,需求中就有,能够分发蓝牙钥匙(通过二维码), 以及扫描二维码,导入钥匙

如果想自己生成二维码做测试,可以用网站:在线二维码图片生成器_二维码扫描软件下载_联图二维码

测试数据格式可以是如下:{"validTo" : "2018-08-26 18:11:38","validFrom" : "2016-08-26 18:11:33", "openTimes" : 0,"name" : "蓝牙钥匙","mac" : "D5:CC:F1:13:92:7C"}  注意:{}要带上

代码如下:

1.先自定义一个透明扫描框

2.在扫描页面

//// 定义属性如下:

@interface ZKScanViewController ()// 扫描二维码的详情页面

@property (strong, nonatomic) AVAudioPlayer *beepPlayer;

@property (strong, nonatomic) CIDetector *detector;

@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;  

///// 在viewDidLoad中

- (void)viewDidLoad {

[super viewDidLoad];

//[self viewDidAppear:YES];

[self UpDateUI];

_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];

}

// Preview

_preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];

_preview.videoGravity =AVLayerVideoGravityResizeAspectFill;

_preview.frame =self.view.layer.bounds;

[self.view.layer insertSublayer:_preview atIndex:0];

// 开始扫描

[_session startRunning];

}

// 直接扫描

#pragma mark AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

NSString *stringValue;

NSLog(@"----array%@",metadataObjects);

// 扫描到对象

if ([metadataObjects count] >0)

{

NSMutableArray *myMutableArray = [metadataObjects mutableCopy];

//停止扫描

[_session stopRunning];

AVMetadataMachineReadableCodeObject * metadataObject = [myMutableArray objectAtIndex:0];

stringValue = metadataObject.stringValue;

NSLog(@"---stringValue---%@",stringValue);

// 过滤

[self filterStr:stringValue];

NSError *error = nil;

NSDictionary *stringdic = [NSJSONSerialization JSONObjectWithData: [stringValue dataUsingEncoding:NSUTF8StringEncoding]

options: NSJSONReadingAllowFragments error:&error];

NSLog(@"-----stringdic----%@",stringdic);

NSLog(@"-----error----%@",error);

// 如果字典为空,弹出提示框

if (stringdic == nil) {

dispatch_async(dispatch_get_main_queue(), ^{

[self alertView];

});

[_coverView removeFromSuperview];

[_session startRunning];

return;

}

// 跳转到下一页面,传值

ZKImportKeyViewController *keyInfoVC = [[ZKImportKeyViewController alloc]init];

keyInfoVC.scanResultDict = stringdic;

[self.navigationController pushViewController:keyInfoVC animated:YES];

[myMutableArray removeAllObjects];

}

}

////过滤空格等

- (void)filterStr:(NSString *)stringValue {

stringValue = [stringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

stringValue = [stringValue stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

stringValue = [stringValue stringByReplacingOccurrencesOfString:@"\n" withString:@""];

stringValue = [stringValue stringByReplacingOccurrencesOfString:@"\t" withString:@""];

}

上一篇下一篇

猜你喜欢

热点阅读