Device APIs

scan

Open the scanner to read QR codes or barcodes.

Overview

my.scan opens the built-in scanner to read QR codes or barcodes. You can choose scan type (QR or bar) and whether to hide the album entry so the user scans only with the camera.

Sample Code

function scan() {
  my.scan({
    type: "qr",
    success: (res) => {
      my.alert({
        title: "Scanned",
        content: res.code || res.qrCode || res.barCode,
      });
    },
    fail: (err) => {
      my.alert({ content: "Scan failed: " + JSON.stringify(err) });
    },
  });
}

Parameters

PropertyTypeRequiredDescription
typeStringNoScan type (default qr):
qr – QR code
bar – Barcode
hideAlbumBooleanNoIf false, user can pick an image from the album to scan. If true, only camera scan is used. Default false.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion.

Success Callback Function

The success callback receives an Object with:

PropertyTypeDescription
codeStringData returned from scanning (generic).
qrCodeStringQR code data returned from scanning.
barCodeStringBarcode data returned from scanning.

Error Codes

ErrorDescription
10User cancelled.
11Operation failure.