Take a screenshot

更新时间:
复制 MD 格式

Capture a screenshot of the screen, viewport, or entire webpage using the snapshot API.

Using snapshot

AlipayJSBridge.call('snapshot', function(result) {
  console.log(result.success);
});

Code example

<h1>Click the buttons below to see different screenshot effects</h1>

<a href="javascript:void(0)" class="btn screen">Take a screenshot of the screen and save it to the gallery</a>
<a href="javascript:void(0)" class="btn viewport">Take a screenshot of the viewport and return a fileURL</a>

<script>
function ready(callback) {
  // If AlipayJSBridge is already injected, call it directly.
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // If not injected, listen for the injection event.
    document.addEventListener('AlipayJSBridgeReady', callback, false);
  }
}
ready(function() {
  document.querySelector('.screen').addEventListener('click', function() {
    AlipayJSBridge.call('snapshot', function(result) {
      alert(JSON.stringify(result));
    });
  });

  document.querySelector('.viewport').addEventListener('click', function() {
    AlipayJSBridge.call('snapshot', {
      range: 'viewport',
      dataType: 'fileURL',
      saveToGallery: false
    }, function(result) {
      alert(JSON.stringify(result));
    });
  });
});
</script>

API reference

AlipayJSBridge.call('snapshot', {
  range, saveToGallery, dataType, imageFormat, quality,
  maxWidth, maxHeight
}, fn)

Request parameters

Property

Type

Description

Required

Default value

range

string

Snapshot area.

  • screen: The full device screen.

  • viewport: The visible area of the webpage.

  • document: The entire webpage.

Important

The document value captures the full webpage and may cause browser memory overflow on Android. Use screen instead.

N

"screen"

saveToGallery

bool

Whether to save the screenshot to the photo gallery.

N

true

dataType

string

Result data format.

  • dataURL: The image data encoded in Base64.

  • fileURL: A temporary file system URL. Purged on exit.

  • none: No data returned. Use when saving to gallery only.

N

"none"

imageFormat

string

jpg or png.

N

"jpg"

quality

int

JPG image quality, from 1 to 100.

N

75

maxWidth

int

Maximum image width. Wider images are scaled down proportionally.

N

-

maxHeight

int

Maximum image height. Taller images are scaled down proportionally.

N

-

fn

function

The callback function.

N

-

Response parameters

The callback receives a result object with the following properties: {success, fileUrl, dataURL}.

Property

Type

Description

success

bool

Whether the operation succeeded.

fileUrl

string

Image URL in the local file system.

dataURL

string

Base64-encoded image data.

Error codes

Error code

Description

10

Failed to save to the photo gallery.

11

Failed to save the image file.