screenshotBase64 method

Future<String> screenshotBase64({
  1. ScreenshotFormat? format,
  2. bool? fullPage,
  3. Rectangle<num>? clip,
  4. int? quality,
  5. bool? omitBackground,
  6. bool? captureBeyondViewport,
  7. bool? fromSurface,
})

Parameters:

  • format: Specify screenshot type, can be either ScreenshotFormat.jpeg or ScreenshotFormat.png. Defaults to 'png'.
  • quality: The quality of the image, between 0-100. Not applicable to png images.
  • fullPage: When true, takes a screenshot of the full scrollable page. Defaults to false.
  • clip: a Rectangle which specifies clipping region of the page.
  • omitBackground: Hides default white background and allows capturing screenshots with transparency. Defaults to false.
  • captureBeyondViewport: Capture the screenshot beyond the viewport. When false, cuts the screenshot by the viewport size. Defaults to true.
  • fromSurface: Captures screenshot from the surface rather than the view. When false, works only in headful mode and ignores page viewport (but not browser window's bounds). Defaults to true.

Returns: Future<String> which resolves to the captured screenshot encoded in base64.

NOTE Screenshots take at least 1/6 second on OS X. See https://crbug.com/741689 for discussion.

Implementation

Future<String> screenshotBase64(
    {ScreenshotFormat? format,
    bool? fullPage,
    Rectangle? clip,
    int? quality,
    bool? omitBackground,
    bool? captureBeyondViewport,
    bool? fromSurface}) {
  final localFormat = format ?? ScreenshotFormat.png;
  final localFullPage = fullPage ?? false;
  captureBeyondViewport ??= true;
  fromSurface ??= true;
  omitBackground ??= false;

  assert(quality == null || localFormat == ScreenshotFormat.jpeg,
      'Quality is only supported for the jpeg screenshots');
  assert(clip == null || !localFullPage, 'clip and fullPage are exclusive');

  return screenshotPool(target.browser).withResource(() async {
    await devTools.target.activateTarget(target.targetID);

    Viewport? roundedClip;
    if (clip != null) {
      roundedClip = Viewport(
          x: clip.left.round(),
          y: clip.top.round(),
          width: (clip.width + clip.left - clip.left.round()).round(),
          height: (clip.height + clip.top - clip.top.round()).round(),
          scale: 1);
    }

    if (localFullPage) {
      var metrics = await devTools.page.getLayoutMetrics();

      // Overwrite clip for full page
      roundedClip = Viewport(
          x: 0,
          y: 0,
          width: metrics.cssContentSize.width.ceil(),
          height: metrics.cssContentSize.height.ceil(),
          scale: 1);
    }
    var shouldSetDefaultBackground =
        omitBackground! && localFormat == ScreenshotFormat.png;
    if (shouldSetDefaultBackground) {
      await devTools.emulation.setDefaultBackgroundColorOverride(
          color: RGBA(r: 0, g: 0, b: 0, a: 0));
    }
    var result = await devTools.page.captureScreenshot(
        format: localFormat.name,
        quality: quality,
        clip: roundedClip,
        captureBeyondViewport: captureBeyondViewport,
        fromSurface: fromSurface);
    if (shouldSetDefaultBackground) {
      await devTools.emulation.setDefaultBackgroundColorOverride();
    }

    if (localFullPage && _viewport != null) {
      await setViewport(_viewport!);
    }

    return result;
  });
}