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.
- (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;
// Set the scan page title
vc.title = @"Standard scan";
// Set the tooltip "Turn on the torch."
vc.torchStateNormalTitle = @"Turn on the torch";
// Set the tooltip "Turn off the torch."
vc.torchStateSelectedTitle = @"Turn off the torch";
// Set the type of the object that you want to scan.
vc.scanType = ScanType_QRCode;
// Set a Select Album button.
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:APCommonUILoadImage(@"camera") style:UIBarButtonItemStylePlain target:self action:@selector(selectPhotos)];
}
- (void)selectPhotos
{
[self.scanVC scanPhotoLibrary];
}
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.
@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 = @"Scan"; // Customize the size of the scan page. CGRect rect = [MPScanCodeViewController constructScanAnimationRect]; self.rectOfInterest = rect; // Customize the Select Album button. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Select Album" 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 { // Customize the view of the scan box. 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]; }
Process scan results.
-(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]; }); }
Continue to scan the code.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // Continue to scan the code. [self resumeScan]; }
- Set a callback that is triggered when the local album fails to be identified.
- (void)scanPhotoFailed { // The callback that is triggered when the album fails to be identified NSLog(@"scanPhotoFailed"); }
Other callback processing
- (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 { // The callback that is triggered when the album fails to be identified NSLog(@"scanPhotoFailed"); }