All Products
Search
Document Center

Advanced guide

Last Updated: May 24, 2021

This topic describes how to use the scan feature in the default UI and the scan feature in a custom UI in baseline 10.1.60 and later based on the official scan demo.

Use scan in the default UI

Modify the parameters on the scan page in the default UI.

  1. - (void)custoDefaultScan {
  2. TBScanViewController *vc = [[MPScanCodeAdapterInterface sharedInstance] createDefaultScanPageWithallback:^(id _Nonnull result, BOOL keepAlive) {
  3. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:result[@"resp_result"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  4. alert.tag = 1001;
  5. [alert show];
  6. }];
  7. [self.navigationController pushViewController:vc animated:YES];
  8. self.scanVC = vc;
  9. // Set the scan page title
  10. vc.title = @"Standard scan";
  11. // Set the tooltip "Turn on the torch."
  12. vc.torchStateNormalTitle = @"Turn on the torch";
  13. // Set the tooltip "Turn off the torch."
  14. vc.torchStateSelectedTitle = @"Turn off the torch";
  15. // Set the type of the object that you want to scan.
  16. vc.scanType = ScanType_QRCode;
  17. // Set a Select Album button.
  18. vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:APCommonUILoadImage(@"camera") style:UIBarButtonItemStylePlain target:self action:@selector(selectPhotos)];
  19. }
  20. - (void)selectPhotos
  21. {
  22. [self.scanVC scanPhotoLibrary];
  23. }

Use Scan in custom UI

If you want to customize the entire scan UI, you can customize the scan page and make it inherit TBScanViewController.

  • Create a scan page and customize the scan area.

    1. @interface MPScanCodeViewController : TBScanViewController <TBScanViewControllerDelegate>
    2. @end
    3. @implementation MPScanCodeViewController
    4. - (instancetype)init
    5. {
    6. if (self = [super init])
    7. {
    8. self.delegate = self;
    9. self.scanType = ScanType_All_Code;
    10. }
    11. return self;
    12. }
    13. - (void)viewDidLoad {
    14. [super viewDidLoad];
    15. // Do any additional setup after loading the view.
    16. self.title = @"Scan";
    17. // Customize the size of the scan page.
    18. CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
    19. self.rectOfInterest = rect;
    20. // Customize the Select Album button.
    21. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Select Album" style:UIBarButtonItemStylePlain target:self action:@selector(selectPhoto)];
    22. }
    23. + (CGRect)constructScanAnimationRect
    24. {
    25. CGSize screenXY = [UIScreen mainScreen].bounds.size;
    26. NSInteger focusFrameWH = screenXY.width / 320 * 220;//as wx
    27. int offet = 10;
    28. if (screenXY.height == 568)
    29. offet = 19;
    30. return CGRectMake((screenXY.width - focusFrameWH) / 2,
    31. (screenXY.height - 64 - focusFrameWH - 83 - 50 - offet) / 2 + 64,
    32. focusFrameWH,
    33. focusFrameWH);
    34. }
    35. -(void)buildContainerView:(UIView*)containerView
    36. {
    37. // Customize the view of the scan box.
    38. UIView* bg = [[UIView alloc] initWithFrame:containerView.bounds];
    39. [containerView addSubview:bg];
    40. CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
    41. UIView* view = [[UIView alloc] initWithFrame:rect];
    42. view.backgroundColor = [UIColor orangeColor];
    43. view.alpha = 0.5;
    44. [bg addSubview:view];
    45. }
    46. - (void)selectPhoto
    47. {
    48. [self scanPhotoLibrary];
    49. }
  • Process scan results.

    1. -(void)didFind:(NSArray<TBScanResult*>*)resultArray
    2. {
    3. TBScanResult *result = resultArray.firstObject;
    4. NSString* content = result.data;
    5. if (result.resultType == TBScanResultTypeQRCode) {
    6. content = [NSString stringWithFormat:@"qrcode:%@, hiddenData:%@, TBScanQRCodeResultType:%@", result.data, result.hiddenData, [result.extData objectForKey:TBScanResultTypeQRCode]];
    7. NSLog(@"subType is %@, ScanType_QRCode is %@", @(result.subType), @(ScanType_QRCode));
    8. } else if (result.resultType == TBScanResultTypeVLGen3Code) {
    9. content = [NSString stringWithFormat:@"gen3:%@", result.data];
    10. NSLog(@"subType is %@, ScanType_GEN3 is %@", @(result.subType), @(ScanType_GEN3));
    11. } else if (result.resultType == TBScanResultTypeGoodsBarcode) {
    12. content = [NSString stringWithFormat:@"barcode:%@", result.data];
    13. NSLog(@"subType is %@, EAN13 is %@", @(result.subType), @(EAN13));
    14. } else if (result.resultType == TBScanResultTypeDataMatrixCode) {
    15. content = [NSString stringWithFormat:@"dm:%@", result.data];
    16. NSLog(@"subType is %@, ScanType_DATAMATRIX is %@", @(result.subType), @(ScanType_DATAMATRIX));
    17. } else if (result.resultType == TBScanResultTypeExpressCode) {
    18. content = [NSString stringWithFormat:@"express:%@", result.data];
    19. NSLog(@"subType is %@, ScanType_FASTMAIL is %@", @(result.subType), @(ScanType_FASTMAIL));
    20. }
    21. dispatch_async(dispatch_get_main_queue(), ^{
    22. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    23. alert.tag = 9999;
    24. [alert show];
    25. });
    26. }
  • Continue to scan the code.

    1. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    2. // Continue to scan the code.
    3. [self resumeScan];
    4. }
  • Set a callback that is triggered when the local album fails to be identified.
    1. - (void)scanPhotoFailed
    2. {
    3. // The callback that is triggered when the album fails to be identified
    4. NSLog(@"scanPhotoFailed");
    5. }
  • Other callback processing

    1. - (void)cameraPermissionDenied
    2. {
    3. [self.navigationController popViewControllerAnimated:YES];
    4. }
    5. - (void)cameraDidStart
    6. {
    7. NSLog(@"started!!");
    8. }
    9. -(void)setTorchState:(TorchState)bState
    10. {
    11. NSLog(@"TorchState:%lu", (unsigned long)bState);
    12. }
    13. -(void)userTrack:(NSString*)name
    14. {
    15. NSLog(@"userTrack:%@", name);
    16. }
    17. -(void)userTrack:(NSString*)name args:(NSDictionary*)data
    18. {
    19. NSLog(@"userTrack:%@, args:%@", name, data);
    20. }
    21. - (void)scanPhotoFailed
    22. {
    23. // The callback that is triggered when the album fails to be identified
    24. NSLog(@"scanPhotoFailed");
    25. }