分享的內(nèi)容,大家看了后,不管有什么問題或者建議,都可以說出來,我都會一一做答,一起加油啦
我的下一篇 <<iOS從相冊讀取二維碼>>需要的話也可以借鑒,原理一樣
我的上一篇<<iOS 二維碼的生成>>也可以學(xué)習(xí)
一般公司業(yè)務(wù)中,二維碼的生成與獲取是同時進行的.比如我們公司做智能門鎖的,需求中就有,能夠分發(fā)藍牙鑰匙(通過二維碼), 以及掃描二維碼,導(dǎo)入鑰匙
如果想自己生成二維碼做測試,可以用網(wǎng)站:在線二維碼圖片生成器_二維碼掃描軟件下載_聯(lián)圖二維碼
測試數(shù)據(jù)格式可以是如下:{"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;
}
// 跳轉(zhuǎn)到下一頁面,傳值
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:@""];
}