Getting Started

更新时间:
复制 MD 格式

This topic describes how to integrate the Scan SDK for Android.

Note

Starting from June 28, 2020, mPaaS no longer maintains the 10.1.32 baseline. Upgrade to the 10.1.60, 10.1.68, or 10.2.3 baseline. Scan supports two connection types: native AAR and component-based (Portal&Bundle). This topic describes how to use the scan feature with the 10.2.3, 10.1.68, and 10.1.60 baselines. Starting from the mPaaS 10.1.68.33 baseline, Scan supports multi-code detection in full-screen mode. The mPaaS 10.2.3 baseline adds a feature that uses AI to detect and automatically magnify small codes.

Prerequisites

Add the SDK

10.2.3

To use the feature that uses AI to detect and automatically magnify small codes, you must install the Scan AI component.

Native AAR

Install the Scan/Scan AI component in your project using AAR Component Management. For more information, see Manage AAR components.

Component-based

In the Portal and Bundle projects, install the Scan/Scan AI component using Component Management. For more information, see Manage component dependencies.

10.1.68/10.1.60

Native AAR

Install the Scan component in your project using AAR Component Management. For more information, see Manage AAR components.

Component-based

In the Portal and Bundle projects, install the Scan component using Component Management. For more information, see Manage component dependencies.

Use the scan feature

10.2.3/10.1.68

Use the full-screen scan feature

ScanRequest scanRequest = new ScanRequest();
MPScan.startMPaasScanFullScreenActivity(this, scanRequest, new MPScanCallbackAdapter() {
    @Override
    public boolean onScanFinish(final Context context, MPScanResult mpScanResult, final MPScanStarter mpScanStarter) {
        Toast.makeText(getApplicationContext(),
                mpScanResult != null ? mpScanResult.getText() : "No code detected", Toast.LENGTH_SHORT).show();
        ((Activity) context).finish();
        // Return true to indicate that the callback is consumed and does not need to be called again.
        return true;
    }
});

Use the window scan feature

On the mPaaS 10.1.68 baseline, you can use the window scan feature (old standard UI). If the scan fails, the system returns to the scan interface. If the scan is successful, the system retrieves the URL from the QR code.

ScanRequest scanRequest = new ScanRequest();
scanRequest.setScanType(ScanRequest.ScanType.QRCODE);
MPScan.startMPaasScanActivity(this, scanRequest, new ScanCallback() {
    @Override
    public void onScanResult(final boolean isProcessed, final Intent result) {
        if (!isProcessed) {
            // The physical Back button or the Back button in the upper-left corner of the scan interface is tapped.
            return;
        }
        // Note: This callback is executed in a subthread.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (result == null || result.getData() == null) {
                    // The scan failed.
                    return;
                }
                // The scan is successful.
                String url = result.getData().toString();
            }
        });
    }
});

10.1.60

On the 10.1.60 baseline, you can use the scan feature. If the scan fails, the system returns to the scan interface. If the scan is successful, the system retrieves the URL from the QR code.

  ScanService service = LauncherApplicationAgent
      .getInstance().getMicroApplicationContext()
      .findServiceByInterface(ScanService.class.getName());

  ScanRequest scanRequest = new ScanRequest();
  scanRequest.setScanType(ScanRequest.ScanType.QRCODE);

  service.scan(this, scanRequest, new ScanCallback() {
      @Override
      public void onScanResult(boolean isProcessed, final Intent result) {
          if (!isProcessed) {
              // The physical Back button or the Back button in the upper-left corner of the scan interface is tapped.
              return;
          }
          // Note: This callback is executed in a subthread.
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  if (result == null || result.getData() == null) {
                      // The scan failed.
                      return;
                  }
                  // The scan is successful.
                  String url = result.getData().toString();
              }
          });
      }
  });