目录

Scanning Barcodes Using the Camera

Service Process

The process of scanning barcodes from images in Bitmap mode is as follows.

  1. A user launches an app and sends a request of scanning a barcode.
  2. The app checks whether it has the camera access and album read permissions.
  3. If the app has the permissions, the scanning API bitMapForSampleBuffer: in HmsBitMap of the HMS Core Scan SDK will be called.
  4. The HMS Core SDK returns the scanning result.
  5. The app checks whether the scanning result is valid. If so, the scanning result is returned; if not, the app adjusts the focal length and scans the barcode again.
  6. The app encapsulates the scanning result and sends it to the user.

Development Process

  1. Obtain an image frame of the camera, that is, sampleBuffer.

    // Obtain the camera sampling data sampleBuffer.
    -(void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
        // Add the sample code in steps 2 and 3. After the processing is complete, release data.
    }

  2. (Optional) Set scanning parameters.

    // QR_CODE | DATA_MATRIX is set for the barcode format, indicating that Scan Kit will support only the QR code and DataMatrix.
    HmsScanOptions *options = [[HmsScanOptions alloc] initWithScanFormatType:QR_CODE | DATA_MATRIX];

    QR_CODE | DATA_MATRIX is set for supporting the QR code and DataMatrix.

  3. Call bitMapForSampleBuffer: withOptions: or multiDecodeBitMapForSampleBuffer: of HmsBitMap to initiate a barcode scanning request and obtain the scanning result in JSON format. For the information contained in this JSON string, please refer to Parsing Barcodes.

    // Obtain the decoding result for a single barcode.
    NSDictionary *dic = [HmsBitMap bitMapForSampleBuffer:sampleBuffer withOptions:options];
    // Obtain the decoding result for multiple barcodes.
    NSArray *list = [HmsBitMap multiDecodeBitMapForSampleBuffer:sampleBuffer withOptions:options];

    For details about the returned value, please refer to Default View.