Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
eddielong
Posts: 9
Registered: ‎02-28-2013
My Carrier: None

Screenshot/Render entire screen

Hi,

 

I have an OpenGL view which is being displayed in a ForeignWindow and a WebView overlay that I want to render to a file. I can do the OpenGL part easily enough but am struggling to find a way to render WebViews to anything useful. If I can get a QByteArray or even render it out to disk I can merge the two later and output an overall screenshot.

 

I've read that QT renders on a separate thread and as such have blocked developers from any rendering operations. Is there any way around this?

 

Appreciate any help.

Please use plain text.
Developer
peter9477
Posts: 6,448
Registered: ‎12-08-2010
My Carrier: none

Re: Screenshot/Render entire screen

I think you have to go right down to the lowest level with libscreen and the screen context APIs, which (I believe) will let you screenshot your own app. If I recall, it would exclude things that are rendered into an entirely different context, which includes system dialogs and the keyboard, but I'm fuzzy on all that.. it's been over a year since I looked at it.

Start here, I think. http://developer.blackberry.com/native/reference/bb10/screen_libref/topic/screen_read_window.html

There may even be examples in the forums if you search using that function name, or related ones. (Check the Native forum though, since it's not related to Cascades.)

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
New Contributor
eddielong
Posts: 9
Registered: ‎02-28-2013
My Carrier: None

Re: Screenshot/Render entire screen

Cheers, yes I did it this way for the OpenGL view using the screen context and window.

However I can't get the context for the device or cascades (at least not that I'm aware of) and so that's where I'm stuck at.

Please use plain text.
Developer
peter9477
Posts: 6,448
Registered: ‎12-08-2010
My Carrier: none

Re: Screenshot/Render entire screen

Do you know whether the "window group" is sufficient to let you get that context? Note that it would be for Cascades: you *cannot* get a context for anything outside of your app, so if by "device" you meant something that would let you screenshot the screen even if your app was not fullscreen: it won't work.

With the window group, you might be able to use http://developer.blackberry.com/native/reference/bb10/screen_libref/topic/screen_join_window_group.h...

The window group name for Cascades can be retrieved using Application's mainWindow property, and that's one of these: http://developer.blackberry.com/cascades/reference/bb__cascades__window.html

I haven't tried this all, but I think the pieces needed are there.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
slashkyle
Posts: 767
Registered: ‎10-16-2012
My Carrier: Telus

Re: Screenshot/Render entire screen

would it be possible to remap the current screenshot keys (up & down volume buttons)  to a button in our apps, so with just 1 simple click a screenshot could be taken?

 

 

Please use plain text.
Developer
peter9477
Posts: 6,448
Registered: ‎12-08-2010
My Carrier: none

Re: Screenshot/Render entire screen

Nope. The current screenshot thing is a system capability, and the fact it can take a picture of the entire screen including possibly confidential user info means it cannot be used programmatically.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
New Contributor
eddielong
Posts: 9
Registered: ‎02-28-2013
My Carrier: None

Re: Screenshot/Render entire screen

[ Edited ]

Hi,

 

Thanks very much for those links you sent, I'm now able to grab the screenshot I need :smileyhappy:

 

Code shared below in case someone else might stumble onto it

 

 

void write_bitmap_header(int nbytes, QByteArray& ba, const int size[])
{
	char header[54];

	/* Set standard bitmap header */
	header[0] = 'B';
	header[1] = 'M';
	header[2] = nbytes & 0xff;
	header[3] = (nbytes >> 8) & 0xff;
	header[4] = (nbytes >> 16) & 0xff;
	header[5] = (nbytes >> 24) & 0xff;
	header[6] = 0;
	header[7] = 0;
	header[8] = 0;
	header[9] = 0;
	header[10] = 54;
	header[11] = 0;
	header[12] = 0;
	header[13] = 0;
	header[14] = 40;
	header[15] = 0;
	header[16] = 0;
	header[17] = 0;
	header[18] = size[0] & 0xff;
	header[19] = (size[0] >> 8) & 0xff;
	header[20] = (size[0] >> 16) & 0xff;
	header[21] = (size[0] >> 24) & 0xff;
	header[22] = -size[1] & 0xff;
	header[23] = (-size[1] >> 8) & 0xff;
	header[24] = (-size[1] >> 16) & 0xff;
	header[25] = (-size[1] >> 24) & 0xff;
	header[26] = 1;
	header[27] = 0;
	header[28] = 32;
	header[29] = 0;
	header[30] = 0;
	header[31] = 0;
	header[32] = 0;
	header[33] = 0;
	header[34] = 0; /* image size*/
	header[35] = 0;
	header[36] = 0;
	header[37] = 0;
	header[38] = 0x9;
	header[39] = 0x88;
	header[40] = 0;
	header[41] = 0;
	header[42] = 0x9l;
	header[43] = 0x88;
	header[44] = 0;
	header[45] = 0;
	header[46] = 0;
	header[47] = 0;
	header[48] = 0;
	header[49] = 0;
	header[50] = 0;
	header[51] = 0;
	header[52] = 0;
	header[53] = 0;

	ba.append(header, sizeof(header));
}

void capture(int width, int height, screen_context_t context)
{
        screen_pixmap_t screen_pix;
	screen_buffer_t screenshot_buf;
	char *screenshot_ptr = NULL;
	int screenshot_stride = 0;

	int usage, format;
	int size[2];

	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] = width;
	size[1] = height;
	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(GLOBALS_PTR()->app()->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");
	QFile outFile("shared/photos/temp1.jpeg");
	outFile.open(QIODevice::WriteOnly);
	image.save(&outFile, "JPEG");

	screen_destroy_pixmap(screen_pix);

Not that this is getting called using a fullscreen context that I have previously created and used as an OpenGL target. Thanks again for the help.

Please use plain text.
Developer
steve8820
Posts: 69
Registered: ‎04-04-2013
My Carrier: Blackberry

Re: Screenshot/Render entire screen

Hi eddielong,

 

I followed your example codes but unfortunately it returns me error stating this "GLOBALS_PTR()->app()->mainWindow()->handle()" returns error something like not declared. My code is exactly the same as yours. Am i doing anything wrong? Please advice.

 

Thank you.

Regards,

Steve Chan
Please use plain text.
Developer
slashkyle
Posts: 767
Registered: ‎10-16-2012
My Carrier: Telus

Re: Screenshot/Render entire screen

[ Edited ]

GLOBALS_PTR()-> is a custom code reference, i belive targeting the OP's desired window 

 

Replacing that entire line with this, worked for me

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

 Also note that this function will currently only save 1 image, and overwrite it each time

 

if you want to save more than one image look into the C++ sprintf function 

Please use plain text.
Developer
steve8820
Posts: 69
Registered: ‎04-04-2013
My Carrier: Blackberry

Re: Screenshot/Render entire screen

Thanks slashkyle. I replaced it with the above code and no more errors. Im now able to save the image into the device. But the image returns 0kb and is not openable. What could possibly do wrong? I even tried your code exactly without any modifications still no luck. Had shared files checked and lscreen added to pro file. Please advice. Thank you.

Regards,

Steve Chan
Please use plain text.