core
    正在准备搜索索引...

    接口 YmxDeviceScan

    扫码

    interface YmxDeviceScan {
        detectCode(data: DetectCodeData): Promise<CodeResult[]>;
        scanCode(data: ScanCodeData): Promise<CodeResult>;
    }

    层级 (查看层级一览)

    索引
    • 识别图片中的二维码、条码,返回结果数组。

      参数

      返回 Promise<CodeResult[]>

      //放一个 id 为 htmlFileInput 文件选择标签到页面上,<input type="file" accept="image/*" id="htmlFileInput" />
      const htmlFileInput = document.getElementById('htmlFileInput');
      if (htmlFileInput.files.length > 0) {
      const reader = new FileReader();
      reader.onload = function(event) {
      const buffer = new Uint8Array(event.target.result);
      //识别
      ymx.detectCode({
      imageData: buffer
      })
      .then(res => {
      alert(JSON.stringify(res));
      })
      .catch(err => {
      alert(JSON.stringify(err));
      });
      };
      reader.onerror = function() {
      alert('读取文件失败');
      };
      //读取文件
      const file = htmlFileInput.files[0];
      reader.readAsArrayBuffer(file);
      } else {
      alert("请选择文件");
      }
      //本地临时文件
      const localFile = 'fs://temp/img.png';
      try {
      //下载图片
      await ymx.downloadFile({
      url: 'https://www.yimenapp.com/assets/img/qr-test.png',
      filePath: localFile
      });
      //识别
      const res = await ymx.detectCode({
      filePath: localFile
      });
      alert(JSON.stringify(res));
      } catch (err) {
      alert(err.message);
      } finally {
      //删除临时文件
      const fs = ymx.getFileSystemManager();
      if (await fs.exist({ path: localFile })) {
      await fs.remove({ path: localFile });
      }
      }
    • 调起客户端扫码界面进行扫码

      参数

      返回 Promise<CodeResult>

      const res = await ymx.scanCode({
      scanType: ['barCode', 'qrCode']
      });
      alert(JSON.stringify(res));
      ymx.scanCode({
      onlyFromCamera: true
      }).then(res => {
      alert(JSON.stringify(res));
      });