美文网首页待处理
mPaaS扫一扫-2021-01-20-周三

mPaaS扫一扫-2021-01-20-周三

作者: 勇往直前888 | 来源:发表于2021-01-21 15:13 被阅读0次

    简介

    扫一扫使用场景越来越广,逐渐成为一个强需求。可以通过原生API来写,感觉稍微有点麻烦。现在mPaaS也提供了扫一扫组件,使用起来就比较方便了。
    这里是简介
    主要意思就是组件功能强大,识别率高;二维码一维码通吃,适用范围广;

    接入

    1. 在 Podfile 文件中,使用 mPaaS_pod "mPaaS_ScanCode" 添加扫码组件依赖。

    2. 执行 pod install 即可完成接入。

    image.png

    更新后,在Pods目录下可以看到头文件:

    image.png

    接口简介

    • 这是一个独立的ViewController,所以是一个独立页面

    • 扫描结果通过一个代理来返回:

    @protocol TBScanViewControllerDelegate <NSObject>
    @required
    
    /**
     ⚠️相机识别在分线程返回,相册识别在主线程返回。
     @param resultArray 识别出来的码值,可能多码。
     */
    - (void)didFind:(NSArray<TBScanResult*>*)resultArray;
    
    // 其他可选的
    @end
    

    扫描结果定义:

    typedef enum : NSUInteger {
        APPROACH_CAMERA = 0,//通过摄像头取帧识别
        APPROACH_LOCALPHOTO = 1,//通过扫描本地图库识别
    } TBScanApproachOfAchieving;
    
    @interface TBScanResult : NSObject
    
    @property (nonatomic, strong) NSString *resultType; // 码类型
    @property (nonatomic, assign) int subType; // 码子类型,一般情况下忽略
    @property (nonatomic, strong) NSString* data; // 码包含的信息
    @property (nonatomic, strong) NSString* hiddenData; // 隐藏码的隐藏信息
    @property (nonatomic, assign) CGRect    rect;//码在预览帧里的位置
    
    @property (nonatomic, assign) TBScanApproachOfAchieving approach; // 识别的途径
    
    @property (nonatomic, strong) NSMutableDictionary *extData; // 额外信息
    
    - (instancetype)initWithTBDecodeResult:(TBDecodeResult *)decodeResult;
    
    @end
    

    使用方式1:组合方式

    把扫描VC当做一个属性,展示扫描界面,实现代理方法,获取扫描结果。
    这种是最简洁的使用方式,推荐

    1. 唤起扫码界面。
     @interface MPScanDemoVC()<TBScanViewControllerDelegate>
     @property(nonatomic, strong) TBScanViewController *scanVC;
     @end
     - (void)startDefauleScanViewController
     {
         TBScanViewController *vc = [[TBScanViewController alloc] init];
         vc.scanType = ScanType_All_Code;
         vc.delegate = self;
         [self.navigationController pushViewController:vc animated:YES];
         self.scanVC = vc;
     }
    
    1. 通过代理方法,处理扫描结果。
     -(void)didFind:(NSArray<TBScanResult*>*)resultArray
     {
         if([resultArray count] > 0) {
             TBScanResult *result = resultArray.firstObject;
             NSString* content = result.data;
             dispatch_async(dispatch_get_main_queue(), ^{
                 // 注意:扫码的结果是在子线程,如有UI相关操作,请切换到主线程
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                 [alert show];
             });
         }
     }
    

    官网使用简介

    使用方式2:继承方式

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

    1. 创建扫码页,并自定义扫码区。
     @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];
      }
    
    1. 处理扫码结果。
     -(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];
          });
      }
    

    官网的例子

    相关文章

      网友评论

        本文标题:mPaaS扫一扫-2021-01-20-周三

        本文链接:https://www.haomeiwen.com/subject/tyzkzktx.html