OneScript Docs
基础

QRCode

二维码 API。

$qrcode.encode(text)

将非空字符串编码为二维码图片,并返回 PNG base64 字符串。

const image = $qrcode.encode("https://example.com")

text 必须是非空字符串。非字符串值会抛出 text must be a string;空字符串会抛出 QR code content must not be empty

$qrcode.decode(image)

从 base64 图片字符串中解析二维码,并返回解析出的文本。

image 可以是纯 base64 字符串,也可以是 data:image/png;base64,... 这类 data URL。常见 Android bitmap 图片格式都支持,包括 PNG、JPEG 和 WebP。

const image = $qrcode.encode("hello")
const text = $qrcode.decode(image)

console.log(text) // "hello"

base64 非法、图片数据不支持,或图片里没有二维码时会抛出错误。

$qrcode.scan(callback)

打开原生相机扫码页。扫描到二维码后,会用解析出的文本调用 callback

$qrcode.scan((text) => {
  console.log(text)
})

scan 使用 callback 风格,不返回 Promise。

$qrcode.scan(options)

使用配置项打开原生相机扫码页。

$qrcode.scan({
  useFrontCamera: false,
  turnOnFlash: false,
  handler(text) {
    console.log(text)
  },
  cancelled() {
    console.log("扫码已取消")
  }
})

useFrontCamera 在支持时使用前置摄像头。turnOnFlash 会为后置摄像头请求开启闪光灯。handler 会在扫码成功后调用。cancelled 会在用户取消扫码、相机权限被拒绝,或扫码页启动失败时调用。

How is this guide?

最后更新于

本页目录