OneScript Docs

QRCode

QR code APIs.

$qrcode.encode(text)

Encodes a non-empty string as a QR code image and returns a PNG base64 string.

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

text must be a non-empty string. Non-string values throw text must be a string; empty strings throw QR code content must not be empty.

$qrcode.decode(image)

Decodes a QR code from a base64 image string and returns the decoded text.

image can be a plain base64 string or a data URL such as data:image/png;base64,.... Common Android bitmap formats are supported, including PNG, JPEG, and WebP.

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

console.log(text) // "hello"

Invalid base64, unsupported image data, or an image without a QR code throws an error.

$qrcode.scan(callback)

Opens the native camera scanner. When a QR code is found, callback is called with the decoded text.

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

scan is callback based and does not return a Promise.

$qrcode.scan(options)

Opens the native camera scanner with options.

$qrcode.scan({
  useFrontCamera: false,
  turnOnFlash: false,
  handler(text) {
    console.log(text)
  },
  cancelled() {
    console.log("Scan cancelled")
  }
})

useFrontCamera selects the front camera when supported. turnOnFlash requests torch mode for the back camera. handler is called when a QR code is scanned. cancelled is called when the scanner is cancelled, camera permission is denied, or the scanner fails to start.

How is this guide?

Last updated on

On this page