// // // // // Created by lbxia on 15/10/21. // Copyright © 2015年 lbxia. All rights reserved. // #import "SubLBXScanViewController.h" #import "LBXScanResult.h" #import "LBXScanWrapper.h" #import "MyQRViewController.h" #import "ScanResultViewController.h" @interface SubLBXScanViewController () @end @implementation SubLBXScanViewController - (void)dealloc { } - (void)viewWillAppear:(BOOL)animated{ self.tabBarController.tabBar.hidden = YES; [super viewWillAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"扫描二维码"; LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init]; style.anmiationStyle = LBXScanViewAnimationStyle_LineMove; style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green"]; self.style = style; if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { // self.edgesForExtendedLayout = UIRectEdgeNone; } self.view.backgroundColor = [UIColor blackColor]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self drawBottomItems]; [self drawTitle]; [self.view bringSubviewToFront:_topTitle]; } - (void)drawTitle { if (!_topTitle) { self.topTitle = [[UILabel alloc]init]; _topTitle.bounds = CGRectMake(0, 0, 145, 60); _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50); //3.5inch iphone if ([UIScreen mainScreen].bounds.size.height <= 568 ) { _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38); _topTitle.font = [UIFont systemFontOfSize:14]; } _topTitle.textAlignment = NSTextAlignmentCenter; _topTitle.numberOfLines = 0; _topTitle.text = @"扫描二维码"; _topTitle.textColor = [UIColor whiteColor]; [self.view addSubview:_topTitle]; } UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 30, 30)]; [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal]; [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:closeButton]; } - (void)closeAction { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)drawBottomItems { if (_bottomItemsView) { return; } self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-164,CGRectGetWidth(self.view.frame), 100)]; _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; [self.view addSubview:_bottomItemsView]; CGSize size = CGSizeMake(65, 87); self.btnFlash = [[UIButton alloc]init]; _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height); _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2); [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal]; [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside]; self.btnPhoto = [[UIButton alloc]init]; _btnPhoto.bounds = _btnFlash.bounds; _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/4, CGRectGetHeight(_bottomItemsView.frame)/2); [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_nor"] forState:UIControlStateNormal]; [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_down"] forState:UIControlStateHighlighted]; [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside]; self.btnMyQR = [[UIButton alloc]init]; _btnMyQR.bounds = _btnFlash.bounds; _btnMyQR.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame) * 3/4, CGRectGetHeight(_bottomItemsView.frame)/2); [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_nor"] forState:UIControlStateNormal]; [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_down"] forState:UIControlStateHighlighted]; [_btnMyQR addTarget:self action:@selector(myQRCode) forControlEvents:UIControlEventTouchUpInside]; [_bottomItemsView addSubview:_btnFlash]; [_bottomItemsView addSubview:_btnPhoto]; // [_bottomItemsView addSubview:_btnMyQR]; } - (void)scanResultWithArray:(NSArray*)array { if (array.count < 1) { [self popAlertMsgWithScanResult:nil]; return; } //经测试,可以同时识别2个二维码,不能同时识别二维码和条形码 for (LBXScanResult *result in array) { NSLog(@"scanResult:%@",result.strScanned); } LBXScanResult *scanResult = array[0]; NSString*strResult = scanResult.strScanned; self.scanImage = scanResult.imgScanned; if (!strResult) { [self popAlertMsgWithScanResult:nil]; return; } //震动提醒 [LBXScanWrapper systemVibrate]; //声音提醒 [LBXScanWrapper systemSound]; [self showNextVCWithScanResult:scanResult]; } - (void)popAlertMsgWithScanResult:(NSString*)strResult { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"识别失败" preferredStyle:UIAlertControllerStyleAlert]; __weak __typeof(self) weakSelf = self; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [weakSelf reStartDevice]; }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } - (void)showNextVCWithScanResult:(LBXScanResult*)strResult { } #pragma mark -底部功能项 //打开相册 - (void)openPhoto { if ([LBXScanWrapper isGetPhotoPermission]) [self openLocalPhoto]; else{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请到设置->隐私中开启本程序相册权限" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } } //开关闪光灯 - (void)openOrCloseFlash { [super openOrCloseFlash]; if (self.isOpenFlash) { [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal]; } else [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal]; } #pragma mark -底部功能项 - (void)myQRCode { MyQRViewController *vc = [MyQRViewController new]; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - 错误提示 - (void)showError:(NSString *)str { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } @end