Overview

HUAWEI Scan Kit scans and parses all major 1D and 2D barcodes and generates QR codes, helping you quickly build barcode scanning functions into your apps.

HUAWEI Scan Kit automatically detects, magnifies, and identifies barcodes from a distance, and is also able to scan a very small barcode in the same way. HUAWEI Scan Kit works even in suboptimal situations, such as under dim lighting or when the barcode is reflective, dirty, blurry, or printed on a cylindrical surface. This leads to a higher scanning success rate, and an improved user experience.

What You Will Create

In this codelab, you will create a barcode scanning app that uses the Default View mode.

What You Will Learn

In this codelab, you will learn how to:

  1. Configure a development environment.
  2. Call barcode scanning capabilities of HUAWEI Scan Kit.

Hardware Requirements

  1. A Mac computer with Xcode installed for app development
  2. An iPhone used for app development and debugging

Software Requirements

  1. Xcode
  2. iOS 9.0 or later

Click the following link to download the demo project of this codelab:

Download

Decompress the downloaded package and use Xcode to open the demo.

If you are using Xcode, you can integrate the HMS Core Scan SDK manually or using pod commands. Before starting development, add the HMS Core Scan SDK to your Xcode project first.

1. Integration Using Pod Commands

1.Edit the Podfile to add pod dependencies.

Podfile example:

Target 'Demo' do pod 'ScanKitFrameWork', '~> 1.0.1.300'

2.Run the pod install command.

$ pod install

2. Manual Integration

1.Download the SDK, and drag ScanKitFrameWorkBundle.bundle and ScanKitFrameWork.framework in the SDK package to the Xcode project, as shown in the following figure.

2.Under TARGETS, configure dependencies for the HMS Core Scan SDK.

Click the plus sign (+), then New Copy Files Phase. A Copy Files area appears. In this area, click the plus sign (+) and select ScanKitFrameWork.framework to add it here. Then select Frameworks for Destination.

3.Add the certificate and configuration file.

A certificate is required for debugging on a real device. Therefore, make sure that the certificate and configuration file match before debugging. It is recommended that you select Automatically manage signing.

Under TARGETS of the demo project, click Signing & Capabilities. Click Debug, expand the Signing node, and select Automatically manage signing.

If you have the required certificate and configuration file (for an enterprise account or an individual account), on the Signing page, select your team and set Bundle Identifier to your own one. If the certificate, configuration file, and Bundle Identifier match, no error message will be displayed. If they do not match, real device-based debugging cannot be performed. Below is an example of incorrect settings:

If you do not have the certificate or configuration file, you can also perform debugging. Apple has taken this into consideration and provided us with several free debugging opportunities. You only need to have an Apple ID.

Go to Xcode > Preferences. Click Accounts, then the plus sign (+) on the bottom. Select Apple ID and click Continue. Enter required information to add an Apple ID. Then you can configure the certificate under Signing.

Set Bundle Identifier (must be unique), and select the team to which your Apple ID belongs. Xcode automatically generates a certificate and configuration file.

If you see the error messages shown in the following figure, the Bundle Identifier value that you entered may be already in use by others. You need to set a new value for Bundle Identifier.

Apple provides only several free debugging opportunities. If you have used up all opportunities, but still need more debugging, use a new Apple ID.

After the certificate is configured successfully, you can press Command+B to try compiling the demo. If no error is reported, the demo can be compiled.

If you have added required permissions, skip this task.
In the demo project directory, select the info.plist file, right-click it, choose Open As > Source Code, and add the permissions to access the camera and album.

<key>NSCameraUsageDescription</key> <string>App needs your consent to access the camera</string> <key>NSPhotoLibraryUsageDescription</key> <string>App needs your consent to access the album</string>

HUAWEI Scan Kit can be called in three modes. Their differences are described in the Development Guide. In this codelab, this Kit is called in Default View mode.

1. Bringing Up the Barcode Scanning Screen in Default View Mode

// After importing <ScanKitFrmameWork/ScanKitFrameWork.h>, call the following code to bring up the barcode scanning screen in Default View mode: HmsScanOptions *options = [[HmsScanOptions alloc] initWithScanFormatType:ALL Photo:FALSE]; HmsDefaultScanViewController *hmsDefaultScanViewController = [[HmsDefaultScanViewController alloc]initDefaultScanWithFormatType:options]; hmsDefaultScanViewController.defaultScanDelegate = self; [self.view addSubview:hmsDefaultScanViewController.view]; [self addChildViewController:hmsDefaultScanViewController]; [self didMoveToParentViewController:hmsDefaultScanViewController]; self.navigationController.navigationBarHidden = YES;

2. Processing the Scanning Result

Process the scanning result in the DefaultScan Delegate callback method. Example:

- (void)defaultScanDelegateForDicResult:(NSDictionary *)resultDic{ [self toastResult:resultDic]; } - (void)defaultScanImagePickerDelegateForImage:(UIImage *)image{ NSDictionary *dic = [HmsBitMap bitMapForImage:image withOptions:[[HmsScanOptions alloc] initWithScanFormatType:ALL Photo:true]]; [self toastResult:dic]; } -(void)toastResult:(NSDictionary *)dic{ [self.navigationController.view hideToastActivity]; if (dic == nil){ [self.view makeToast:@"Scanning code not recognized" duration:1.0 position:@"CSToastPositionCenter" boolToast:YES]; return; } NSString *string = [NSString stringWithFormat:@"%@", [dic objectForKey:@"text"]]; if ([string length] == 0){ [self.view makeToast:@"Scanning code not recognized" duration:1 position:@"CSToastPositionCenter" boolToast:YES]; return; } NSString *toastString = [NSString stringWithFormat:@"CodeFormat=%@; ResultType=%@; Result=%@", [dic objectForKey:@"formatValue"], [dic objectForKey:@"sceneType"], [dic objectForKey:@"text"]]; [self.view makeToast:toastString duration:2.0 position:@"CSToastPositionCenter" boolToast:YES]; }

After the development is complete, click Run to run your Xcode project to generate an IPA file, and install it on your phone for testing.

After launching the app, you'll see its home screen.

Tap DefaultView. You'll see the following screen.

Scan a barcode, and a result is returned.

Well done. You have successfully completed this codelab and learned how to:

  1. Integrate HUAWEI Scan Kit.
  2. Use the Default View mode of HUAWEI Scan Kit to scan barcodes.

To learn more about HUAWEI Scan Kit, visit the HUAWEI Developers website. If you have any questions during the development, try to find answers from the FAQs that we provide for this Kit.

For more information, please click the following links:

  1. Development Guide
  2. API Reference
Code copied