进阶指南

本文将结合 扫一扫 官方 Demo 介绍如何使用扫码功能。

标准 UI 下使用扫一扫

在标准 UI 下修改扫码所在页面的参数。

```objectivec
- (void)custoDefaultScan {
   TBScanViewController *vc = [[MPScanCodeAdapterInterface sharedInstance] createDefaultScanPageWithallback:^(id  _Nonnull result, BOOL keepAlive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:result[@"resp_result"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        alert.tag = 1001;
        [alert show];
    }];
    [self.navigationController pushViewController:vc animated:YES];
    self.scanVC =  vc;

    // 设置扫码界面 title
    vc.title = @"标准扫码";

    // 设置打开手电筒提示文字
    vc.torchStateNormalTitle = @"打开手电筒";

    // 设置关闭手电筒提示文字
    vc.torchStateSelectedTitle = @"关闭手电筒";

    // 设置扫码识别类型
    vc.scanType =  ScanType_QRCode;

    // 设置选择相册按钮
    vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:APCommonUILoadImage(@"camera") style:UIBarButtonItemStylePlain target:self action:@selector(selectPhotos)];

}

- (void)selectPhotos
{
    [self.scanVC scanPhotoLibrary];
}
```

自定义 UI 下使用扫一扫

若您需要完全自定义扫码 UI 界面,可自定义扫码页并让其继承 TBScanViewController。

  • 创建扫码页,并自定义扫码区。

      @interface MPScanCodeViewController : TBScanViewController <TBScanViewControllerDelegate>
    
      @end
    
      @implementation MPScanCodeViewController
    
      - (instancetype)init
      {
          if (self = [super init])
          {
              self.delegate = self;
              self.scanType = ScanType_All_Code;
          }
          return self;
      }
    
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
          self.title = @"扫码";
    
          // 自定义扫码界面大小
          CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
          self.rectOfInterest = rect;
    
          // 自定义相册按钮
          self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"选择相册" style:UIBarButtonItemStylePlain target:self action:@selector(selectPhoto)];
      }
    
      + (CGRect)constructScanAnimationRect
      {
          CGSize screenXY = [UIScreen mainScreen].bounds.size;
          NSInteger focusFrameWH = screenXY.width / 320 * 220;//as wx
          int offet = 10;
          if (screenXY.height == 568)
              offet = 19;
    
          return CGRectMake((screenXY.width - focusFrameWH) / 2,
                            (screenXY.height - 64 - focusFrameWH - 83 - 50 - offet) / 2 + 64,
                            focusFrameWH,
                            focusFrameWH);
      }
    
      -(void)buildContainerView:(UIView*)containerView
      {
          // 自定义扫码框 view
          UIView* bg = [[UIView alloc] initWithFrame:containerView.bounds];
          [containerView addSubview:bg];
          CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
          UIView* view = [[UIView alloc] initWithFrame:rect];
          view.backgroundColor = [UIColor orangeColor];
          view.alpha = 0.5;
          [bg addSubview:view];
      }
    
      - (void)selectPhoto
      {
          [self scanPhotoLibrary];
      }
  • 处理扫码结果。

      -(void)didFind:(NSArray<TBScanResult*>*)resultArray
      {
          TBScanResult *result = resultArray.firstObject;
          NSString* content = result.data;
          if (result.resultType == TBScanResultTypeQRCode) {
              content = [NSString stringWithFormat:@"qrcode:%@, hiddenData:%@, TBScanQRCodeResultType:%@", result.data, result.hiddenData, [result.extData objectForKey:TBScanResultTypeQRCode]];
              NSLog(@"subType is %@, ScanType_QRCode is %@", @(result.subType), @(ScanType_QRCode));
          } else if (result.resultType == TBScanResultTypeVLGen3Code) {
              content = [NSString stringWithFormat:@"gen3:%@", result.data];
              NSLog(@"subType is %@, ScanType_GEN3 is %@", @(result.subType), @(ScanType_GEN3));
          } else if (result.resultType == TBScanResultTypeGoodsBarcode) {
              content = [NSString stringWithFormat:@"barcode:%@", result.data];
              NSLog(@"subType is %@, EAN13 is %@", @(result.subType), @(EAN13));
          } else if (result.resultType == TBScanResultTypeDataMatrixCode) {
              content = [NSString stringWithFormat:@"dm:%@", result.data];
              NSLog(@"subType is %@, ScanType_DATAMATRIX is %@", @(result.subType), @(ScanType_DATAMATRIX));
          } else if (result.resultType == TBScanResultTypeExpressCode) {
              content = [NSString stringWithFormat:@"express:%@", result.data];
              NSLog(@"subType is %@, ScanType_FASTMAIL is %@", @(result.subType), @(ScanType_FASTMAIL));
          }
          dispatch_async(dispatch_get_main_queue(), ^{
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
              alert.tag = 9999;
              [alert show];
          });
      }
  • 持续扫码。

      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
          // 持续扫码
          [self resumeScan];
      }
  • 本地相册识别失败的回调。

      - (void)scanPhotoFailed
      {
          // 相册识别失败的回调
          NSLog(@"scanPhotoFailed");
      }
  • 其他回调处理。

      - (void)cameraPermissionDenied
      {
          [self.navigationController popViewControllerAnimated:YES];
      }
    
      - (void)cameraDidStart
      {
          NSLog(@"started!!");
      }
    
      -(void)setTorchState:(TorchState)bState
      {
          NSLog(@"TorchState:%lu", (unsigned long)bState);
      }
    
      -(void)userTrack:(NSString*)name
      {
          NSLog(@"userTrack:%@", name);
      }
    
      -(void)userTrack:(NSString*)name args:(NSDictionary*)data
      {
          NSLog(@"userTrack:%@, args:%@", name, data);
      }
    
      - (void)scanPhotoFailed
      {
          // 相册识别失败的回调
          NSLog(@"scanPhotoFailed");
      }