QRCodeScanForChangeDeviceViewController.m 30 KB

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