QRCodeScanForChangeDeviceViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. //
  2. // QRCodeScanForChangeDeviceViewController.m
  3. // 隐私保护
  4. //
  5. // Created by APPLE on 2023/9/19.
  6. //
  7. #import "QRCodeScanForChangeDeviceViewController.h"
  8. #import <AVFoundation/AVFoundation.h>
  9. #import <ImageIO/ImageIO.h>
  10. #import "RSATool.h"
  11. #import "connectDeviceManager.h"
  12. #import "uploadFileManager.h"
  13. #import "downloadManager.h"
  14. #import "backupsFileManager.h"
  15. #import <TZImageManager.h>
  16. #import <TZImagePickerController.h>
  17. #import "boxDownloadFileManager.h"
  18. @interface QRCodeScanForChangeDeviceViewController ()<AVCaptureMetadataOutputObjectsDelegate,AVCaptureVideoDataOutputSampleBufferDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,TZImagePickerControllerDelegate>
  19. @property(nonatomic,strong)AVCaptureVideoPreviewLayer *layer;
  20. //捕捉会话
  21. @property(nonatomic,strong)AVCaptureSession *session;
  22. //辅助区域框
  23. @property(nonatomic,strong)UIImageView * cyanEdgeImageView;
  24. @property(nonatomic,strong)UIImageView * qrCodeScanLine;
  25. @property(nonatomic,strong)NSTimer * scanLineTimer;
  26. ////打开灯光btn,默认为hidden
  27. //
  28. //@property(nonatomic,strong)UIButton * lightBtn;
  29. //
  30. ////获取相册图片进行扫描
  31. //
  32. //@property(nonatomic,strong)UIButton * photoBtn;
  33. @end
  34. @implementation QRCodeScanForChangeDeviceViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. [self.navigationBar setHidden:YES];
  39. [self.toolBar setHidden:YES];
  40. AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  41. //判断摄像头状态是否可用
  42. if(authStatus==AVAuthorizationStatusAuthorized){
  43. [self startScan];
  44. }else{
  45. NSLog(@"未开启相机权限,请前往设置中开启");
  46. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  47. if (granted){
  48. mainBlock(^{
  49. [self startScan];
  50. });
  51. }
  52. }];
  53. }
  54. }
  55. -(void)viewWillDisappear:(BOOL)animated
  56. {
  57. [super viewWillDisappear:animated];
  58. if(_scanLineTimer)[_scanLineTimer invalidate];
  59. }
  60. - (void)backBtnPressed
  61. {
  62. [super backBtnPressed];
  63. if(_isRootVCType){
  64. //发个通知
  65. [[NSNotificationCenter defaultCenter] postNotificationName:scanChangeDeviceBackNotification object:nil];
  66. }
  67. }
  68. //开始扫描二维码
  69. -(void)startScan{
  70. //1.创建捕捉会话,AVCaptureSession是第一个要被创建的对象,所有的操作都要基于这一个session
  71. self.session = [[AVCaptureSession alloc]init];
  72. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  73. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
  74. [self.session addInput:input];
  75. //3.添加输出数据(示例对象-->类对象-->元类对象-->根元类对象)
  76. /*输入的类是AVCaptureInput,那么输出的类相应的就应该是AVCaptureOutput。
  77.  输出不需要和设备挂钩,因为一般情况下,我们的输出要么是音频或视频文件,要么是一些其他的数据,像二维码扫描一般是字符串类型。
  78.  所以创建AVCaptureOutput实例就不需要AVCaptureDevice对象。
  79.  AVCaptureOutput也同样是一个抽象类,同样要使用其子类,在这里我们扫描二维码,
  80.  使用的是AVCaptureMetadataOutput,设置代码如下所示*/
  81. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  82. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  83. //设置能扫描的区域,这里注意,CGRectMake的x,y和width,height的值是互换位置
  84. output.rectOfInterest=CGRectMake(250/self.view.frame.size.height, 100/self.view.frame.size.width, (self.view.frame.size.width-200)/self.view.frame.size.width, (self.view.frame.size.width-200)/self.view.frame.size.width);
  85. [self.session addOutput:output];
  86. //设置输入元数据的类型(类型是二维码,条形码数据,注意,这个一定要写在添加到session后面,不然要崩溃,如果只需要扫描二维码只需要AVMetadataObjectTypeQRCode,如果还需要扫描条形码,那么全部添加上)
  87. [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,
  88. AVMetadataObjectTypeEAN8Code,
  89. AVMetadataObjectTypeEAN13Code,
  90. AVMetadataObjectTypeCode39Code,
  91. AVMetadataObjectTypeCode39Mod43Code,
  92. AVMetadataObjectTypeCode93Code,
  93. AVMetadataObjectTypeCode128Code,
  94. AVMetadataObjectTypePDF417Code,
  95. AVMetadataObjectTypeAztecCode,
  96. AVMetadataObjectTypeUPCECode,
  97. AVMetadataObjectTypeInterleaved2of5Code,
  98. AVMetadataObjectTypeITF14Code,
  99. AVMetadataObjectTypeDataMatrixCode,
  100. ]];
  101. //4.添加扫描图层
  102. self.layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
  103. self.layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
  104. self.layer.frame = self.view.bounds;
  105. [self.view.layer addSublayer:self.layer];
  106. //5.创建view,通过layer层进行设置边框宽度和颜色,用来辅助展示扫描的区域
  107. _cyanEdgeImageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 250, self.view.frame.size.width-200, self.view.frame.size.width-200)];
  108. // _cyanEdgeImageView.layer.borderWidth=2;
  109. //
  110. // _cyanEdgeImageView.layer.borderColor =[UIColor cyanColor].CGColor;
  111. _cyanEdgeImageView.image = [UIImage imageNamed:@"qrCode_scan_bg"];
  112. [self.view addSubview:_cyanEdgeImageView];
  113. _qrCodeScanLine=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-200, 1)];
  114. _qrCodeScanLine.image = [UIImage imageNamed:@"qrCode_scan_line"];
  115. [_cyanEdgeImageView addSubview:_qrCodeScanLine];
  116. _scanLineTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(scanLineDownAndUpFun) userInfo:nil repeats:YES];
  117. UILabel *tipLib = [[UILabel alloc] initWithFrame:CGRectMake(20, _cyanEdgeImageView.hw_max_y + 15, SCREEN_W - 40, 20)];
  118. tipLib.text = NSLocalizedString(@"guide_qrcoede_tips_please",nil);
  119. tipLib.font = [UIFont systemFontOfSize:14.0];
  120. tipLib.textAlignment = NSTextAlignmentCenter;
  121. tipLib.textColor = [UIColor whiteColor];
  122. [self.view addSubview:tipLib];
  123. //6.创建检测光感源
  124. AVCaptureVideoDataOutput *guangOutPut = [[AVCaptureVideoDataOutput alloc] init];
  125. [guangOutPut setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  126. //设置为高质量采集率
  127. [self.session setSessionPreset:AVCaptureSessionPresetHigh];
  128. //把光感源添加到会话
  129. [self.session addOutput:guangOutPut];
  130. // self.lightBtn=[[UIButton alloc]initWithFrame:CGRectMake(150, cyanView.frame.origin.y+cyanView.frame.size.height+100, self.view.frame.size.width-300, self.view.frame.size.width-300)];
  131. //
  132. // self.lightBtn.backgroundColor=[UIColor grayColor];
  133. //
  134. // [self.lightBtn addTarget:self action:@selector(lightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  135. //
  136. // [self.view addSubview:self.lightBtn];
  137. // self.photoBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, self.lightBtn.frame.origin.y+self.lightBtn.frame.size.height+50, self.view.frame.size.width-200, 50)];
  138. //打开相册
  139. UIButton *openAlbunBut = [[UIButton alloc] init];
  140. [openAlbunBut setTitle:NSLocalizedString(@"guide_qrcoede_open_album",nil) forState:UIControlStateNormal];
  141. [openAlbunBut setTitleColor:[UIColor hwColor:@"#3B7FFF" alpha:1.0] forState:UIControlStateNormal];
  142. openAlbunBut.titleLabel.font = [UIFont systemFontOfSize:14.0];
  143. [openAlbunBut addTarget:self action:@selector(photoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  144. openAlbunBut.backgroundColor = [UIColor hwColor:@"#FFFFFF" alpha:0.2];
  145. [self.view addSubview:openAlbunBut];
  146. openAlbunBut.layer.cornerRadius = 24;
  147. openAlbunBut.layer.masksToBounds = YES;
  148. [openAlbunBut mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.width.mas_equalTo(140);
  150. make.height.mas_equalTo(48);
  151. make.centerX.mas_equalTo(0);
  152. make.bottom.mas_equalTo(-80);
  153. }];
  154. // [self.photoBtn setTitle:@"相册" forState:UIControlStateNormal];
  155. // [self.photoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  156. // self.photoBtn.backgroundColor=[UIColor orangeColor];
  157. // [self.photoBtn addTarget:self action:@selector(photoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  158. // [self.view addSubview:self.photoBtn];
  159. //9.开始扫描
  160. [self.session startRunning];
  161. //添加返回键
  162. CGFloat btn_w_h = 40;
  163. CGFloat btn_show = 28;
  164. UIButton *backBtn = [[UIButton alloc] init];
  165. [self.view addSubview:backBtn];
  166. [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  167. make.top.mas_equalTo(H_STATE_BAR + (64.f - btn_w_h)/2.f);
  168. make.left.mas_equalTo(10);
  169. make.width.mas_equalTo(btn_w_h);
  170. make.height.mas_equalTo(btn_w_h);
  171. }];
  172. [backBtn setImage:[UIImage imageNamed:@"icon_base_back"] forState:(UIControlStateNormal)];
  173. [backBtn setImageEdgeInsets:(UIEdgeInsetsMake((btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f, (btn_w_h - btn_show)/2.f))];
  174. [backBtn addTarget:self
  175. action:@selector(backBtnPressed)
  176. forControlEvents:(UIControlEventTouchUpInside)];
  177. }
  178. #pragma mark timer 处理线上下移动
  179. bool isDownType22 = YES;
  180. -(void)scanLineDownAndUpFun
  181. {
  182. [UIView animateWithDuration:0.01 animations:^{
  183. if(isDownType22 && self->_qrCodeScanLine.hw_y <= self->_cyanEdgeImageView.hw_h){
  184. self->_qrCodeScanLine.hw_y += 2;
  185. if(self->_cyanEdgeImageView.hw_h - self->_qrCodeScanLine.hw_y <= 5){
  186. isDownType22 = NO;
  187. }
  188. }
  189. else if(!isDownType22 && self->_qrCodeScanLine.hw_y >= 0)
  190. {
  191. self->_qrCodeScanLine.hw_y -= 2;
  192. if(self->_qrCodeScanLine.hw_y <= 5){
  193. isDownType22 = YES;
  194. }
  195. }
  196. }];
  197. }
  198. //实现扫描的回调代理方法
  199. - (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection{
  200. //如果数组metadataObjects中有数据,metadataObjects是个数组类型
  201. if(metadataObjects.count>0) {
  202. AVMetadataMachineReadableCodeObject*object = [metadataObjects lastObject];
  203. NSLog(@"%@",object.stringValue);
  204. /*扫描到有用信息时取消扫描*/
  205. NSString *resStr = object.stringValue;//RK3908P1V62112465
  206. [self handleScanCodeResultFun:resStr];
  207. }else{
  208. //[[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_error",nil)] show];
  209. NSLog(@"没有扫描到数据");
  210. }
  211. }
  212. #pragma mark 处理扫码出来的数据
  213. - (void)handleScanCodeResultFun:(NSString*)resultStr
  214. {
  215. [[uploadFileManager shareInstance] suspendUploadFileFun:YES withModel:nil];
  216. [[downloadManager shareInstance] suspendDownloadFileFun:YES withModel:nil];
  217. [[backupsFileManager shareInstance] suspendBackupsFileFun];
  218. [[boxDownloadFileManager shareInstance] suspendDownloadFileFun:YES withModel:nil];
  219. NSString *resStr = resultStr;
  220. if(resStr.length > 22)
  221. {
  222. NSString*desStr = [RSATool AES128Decrypt:resStr key:AESCODEKEEYY];
  223. if(desStr){//能解码
  224. resStr = desStr;
  225. }
  226. }
  227. //Ch0IKJCY9lOgokjfPBFE1cowdC1+tj7ywB2pZgSzjhc=
  228. //sgl5OzDoVjY5SmGuL50/EnQ4n6Kw+DzRiE1oUjq7yAM=
  229. if (([resStr containsString:@"RK"] && (resStr.length == @"RK3908P1V62112465".length))
  230. || resStr.length == @"0333933700223250017273".length
  231. ){
  232. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  233. if (!deviceDict || [[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){
  234. NSString *oldStr = deviceDict[Const_Have_Add_Device_SN];
  235. if([oldStr isEqualToString:resStr]){
  236. [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_change_same",nil)] show];
  237. //停止扫描
  238. [self.session stopRunning];
  239. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  240. [self.session startRunning];
  241. });
  242. return;
  243. }
  244. }
  245. //[[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_ok",nil)] show];
  246. [_scanLineTimer invalidate];
  247. //停止扫描
  248. [self.session stopRunning];
  249. //移除扫描层layer
  250. [self.layer removeFromSuperlayer];
  251. [self showNewIndicatorHaveStrWithCanBack:NO canTouch:NO showText:NSLocalizedString(@"guide_qrcoede_tips_ok",nil)];
  252. [[connectDeviceManager shareInstance] getThridMsgBySN:resStr needReconnect:YES didNetEnd:^(NSInteger didSuc) {
  253. [self removeNewIndicatorHaveStr];
  254. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable)
  255. {
  256. [[iToast makeText:NSLocalizedString(@"phone_network_fail_Tips",nil)] show];
  257. [self.navigationController popViewControllerAnimated:YES];
  258. return;
  259. }
  260. if(didSuc == 1){
  261. //数据埋点
  262. [[netWorkManager shareInstance] DataEmbeddingPointBy:2 withEventValue:@"Change_device"];
  263. NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:deviceDict];
  264. [dict setObject:resStr forKey:Const_Have_Add_Device_SN];
  265. [HWDataManager setObjectWithKey:Const_Have_Add_Device value:dict];
  266. BOOL didSetBackWork = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_didSet_working_background)];
  267. if(!didSetBackWork){
  268. [HWDataManager setBoolWithKey:stringKeyAddSn(Const_file_Transfe_didSet_working_background) value:YES];
  269. [HWDataManager setBoolWithKey:stringKeyAddSn(Const_file_Transfe_working_background) value:YES];
  270. }
  271. [webSocketManager shareInstance].isChangeBoxType = YES;
  272. [[webSocketManager shareInstance].commandChannelManager rc_close];
  273. [webSocketManager shareInstance].commandChannelManager = nil;
  274. //尝试修复偶现扫码切换云机 已经扫码到 要输新SN密码 但是云机显示是旧SN的 延时一秒跳转
  275. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  276. //清除原来设置的数据
  277. ksharedAppDelegate.isWebSockLinkOKAginType = NO;
  278. ksharedAppDelegate.cloudPhoneExtraFileListMod = nil;
  279. ksharedAppDelegate.DisabledFileTransferType = NO;
  280. ksharedAppDelegate.isImageNewFor130 = NO;
  281. ksharedAppDelegate.isNeedShowImageNewType = NO;
  282. ksharedAppDelegate.TvStatusMod = nil;
  283. ksharedAppDelegate.NASMsgMod = nil;
  284. ksharedAppDelegate.isDidShowPwdType = NO;
  285. ksharedAppDelegate.isDidShowPwdType = NO;
  286. ksharedAppDelegate.NASFileByBoxService = nil;
  287. ksharedAppDelegate.NASShareFileService = nil;
  288. HLog(@"isWebSockLinkOKAginType NO")
  289. [[webSocketManager shareInstance] opencommandChannelManagerrc_openURL];
  290. [[NSNotificationCenter defaultCenter] postNotificationName:GuideOkNotification object:nil];
  291. });
  292. }else{
  293. [webSocketManager shareInstance].isChangeBoxType = NO;
  294. // if(didSuc == 2){
  295. // [[iToast makeText:NSLocalizedString(@"scan_sn_data_error_tip",nil)] show];
  296. // }
  297. // else{
  298. // [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_get_SdnID_fail",nil)] show];
  299. // }
  300. if(self->_didScanErrorFun){
  301. self->_didScanErrorFun(didSuc);
  302. }
  303. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  304. mainBlock(^{
  305. [self.navigationController popViewControllerAnimated:YES];
  306. });
  307. });
  308. }
  309. }];
  310. }
  311. else{
  312. [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_error",nil)] show];
  313. }
  314. }
  315. //光感传感器代理
  316. -(void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection{
  317. //获取光线的值
  318. CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,sampleBuffer, kCMAttachmentMode_ShouldPropagate);
  319. NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(__bridge NSDictionary*)metadataDict];
  320. CFRelease(metadataDict);
  321. NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
  322. float brightnessValue = [[exifMetadata objectForKey:(NSString*)kCGImagePropertyExifBrightnessValue]floatValue];
  323. NSLog(@"%f",brightnessValue);
  324. // 根据brightnessValue的值来打开和关闭闪光灯,一般值小于0就需要打开,大于0就关闭
  325. // if((brightnessValue <0)) {//显示闪光灯
  326. // self.lightBtn.hidden=NO;
  327. // }else if((brightnessValue >0)) {//隐藏闪光灯
  328. // self.lightBtn.hidden=YES;
  329. // }
  330. }
  331. ////闪光灯按钮点击方法
  332. //
  333. //-(void)lightBtnClick:(UIButton*)sender{
  334. // //判断当前设备是否有闪光灯
  335. //
  336. // AVCaptureDevice * device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  337. //
  338. // BOOL result=[device hasTorch];
  339. //
  340. // if(result==YES){
  341. // if(self.lightBtn.isSelected==NO){
  342. // self.lightBtn.selected=YES;
  343. // self.lightBtn.backgroundColor=[UIColor greenColor];
  344. // [device lockForConfiguration:nil];
  345. // [device setTorchMode: AVCaptureTorchModeOn];//开
  346. // [device unlockForConfiguration];
  347. // }else if(self.lightBtn.isSelected==YES){
  348. // self.lightBtn.selected=NO;
  349. // self.lightBtn.backgroundColor=[UIColor grayColor];
  350. // [device lockForConfiguration:nil];
  351. // [device setTorchMode: AVCaptureTorchModeOff];//关
  352. // [device unlockForConfiguration];
  353. // }
  354. // }else{
  355. // NSLog(@"当前设备闪光灯不可用");
  356. // }
  357. //}
  358. //
  359. ////获取相册图片btn点击方法
  360. -(void)photoBtnClick:(UIButton*)sender{
  361. // UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  362. // imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  363. // // imagePicker.allowsEditing = YES;
  364. // imagePicker.delegate=self;
  365. // [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
  366. [self photoClickFun];
  367. }
  368. - (void)photoClickFun
  369. {
  370. TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
  371. imagePicker.allowPickingVideo=NO;
  372. //imagePicker.isStatusBarDefault=YES;
  373. imagePicker.allowTakePicture=NO;
  374. imagePicker.allowPreview = NO;
  375. // 4. 照片排列按修改时间升序
  376. imagePicker.sortAscendingByModificationDate = YES;
  377. [self presentViewController:imagePicker animated:YES completion:nil];
  378. }
  379. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto
  380. {
  381. if(photos && photos.count >= 1){
  382. [self detectImageFunBy:photos.firstObject];
  383. }
  384. else{
  385. [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_error",nil)] show];
  386. }
  387. }
  388. //UIImagePickerControllerDelegate选择图片的回调
  389. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  390. //把UIimage类型转换成CIimage类型
  391. UIImage *pickedImage = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
  392. [picker dismissViewControllerAnimated:YES completion:^{
  393. }];
  394. [self detectImageFunBy:pickedImage];
  395. }
  396. #pragma mark 解析Uiimage
  397. - (void)detectImageFunBy:(UIImage*)pickedImage
  398. {
  399. CIImage*detectImage = [CIImage imageWithData:UIImagePNGRepresentation(pickedImage)];
  400. //解析扫描二维码结果字符串
  401. CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
  402. //CIQRCodeFeature*feature = (CIQRCodeFeature*)[detector featuresInImage:detectImage options:nil].firstObject;
  403. NSArray *messageStringArr = (CIQRCodeFeature*)[detector featuresInImage:detectImage options:nil];
  404. //排除掉 http的
  405. __block NSString *curQRCodeStr = nil;
  406. for(CIQRCodeFeature*feature in messageStringArr){
  407. if([feature.messageString rangeOfString:@"http"].location == NSNotFound){
  408. curQRCodeStr = feature.messageString;
  409. break;
  410. }
  411. }
  412. if(curQRCodeStr) {
  413. NSLog(@"=================二维码结果为%@",curQRCodeStr);
  414. [self handleScanCodeResultFun:curQRCodeStr];
  415. }else{
  416. [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_error",nil)] show];
  417. NSLog(@"没有扫描到数据");
  418. }
  419. // //停止会话对象扫描
  420. // [self.session stopRunning];
  421. // //移除扫描层layer
  422. // [self.layer removeFromSuperlayer];
  423. }
  424. @end