-1

is it possible to take a screenshot of a container like DockLayout and all of its children programmatically in BB10 (native)? I've found the Screenshot class in the docs, but it's only possible to take a display or app window screenshot...

Any tips or hints?

Thanks.

pawlinsky
  • 420
  • 1
  • 6
  • 18

1 Answers1

0

This is the basic code I use for capturing an area of the screen under program control.

Note that you can only capture a screen displayed by the program this code is in.

        void ScreenCapture::capture(qreal x, qreal y, qreal width, qreal height)
    {
        smDebug(1) << "Capturing" << x << y << width << height;

        screen_pixmap_t screen_pix;
        screen_buffer_t screenshot_buf;
        screen_context_t context;
        char *screenshot_ptr = NULL;
        int screenshot_stride = 0;
        int usage, format;
        int size[2];

        screen_create_context(&context, 0);
        screen_create_pixmap(&screen_pix, context);

        usage = SCREEN_USAGE_READ | SCREEN_USAGE_NATIVE;
        screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_USAGE, &usage);

        format = SCREEN_FORMAT_RGBA8888;
        screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_FORMAT, &format);

        size[0] = 0;
        size[1] = 0;

        screen_get_window_property_iv(Application::instance()->mainWindow()->handle(), SCREEN_PROPERTY_SIZE, size);
        smDebug(1) << size[0] << 'x' << size[1];

        screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_BUFFER_SIZE, size);

        screen_create_pixmap_buffer(screen_pix);

        screen_get_pixmap_property_pv(screen_pix, SCREEN_PROPERTY_RENDER_BUFFERS,
                (void**)&screenshot_buf);

        screen_get_buffer_property_pv(screenshot_buf, SCREEN_PROPERTY_POINTER,
                (void**)&screenshot_ptr);

        screen_get_buffer_property_iv(screenshot_buf, SCREEN_PROPERTY_STRIDE,
                &screenshot_stride);

        screen_read_window(Application::instance()->mainWindow()->handle(), screenshot_buf, 0, NULL ,0);

        QByteArray array;
        int nbytes = size[0] * size[1] * 4;
        write_bitmap_header(nbytes, array, size);
        for (int i = 0; i < size[1]; i++)
        {
            array.append(screenshot_ptr + i * screenshot_stride, size[0] * 4);
        }

        QImage image = QImage::fromData(array, "BMP");

        image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
        baseImage = QImage(width,height,QImage::Format_ARGB32_Premultiplied);
        baseImage.painter().drawImage(0, 0, image, x, y, width, height);
        if (mWaterMarkImage != NULL)
            baseImage.updateToView(mWaterMarkImage);
        soundplayer_play_sound("event_camera_shutter");
        screen_destroy_pixmap(screen_pix);
        smDebug(1) << "capture done.";
    }
Richard
  • 8,920
  • 2
  • 18
  • 24