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
Developer
steve8820
Posts: 75
Registered: ‎04-04-2013
My Device: BB10 Dev Alpha Simulator
My Carrier: Blackberry

Re: Copy current screen image to file

Hi Steven,

 

Sorry about the previous post. I manage to solve it with this #include <bb/cascades/Window>. Althought no errors now but when i do the screenshot, the image saved in the folder is 0kb. Any suggestion?

 

Thank you.

Regards,

Steve Chan
Please use plain text.
Developer
StevenKader
Posts: 634
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: Copy current screen image to file

All of the permissions are set properly?  Which device please?


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Please use plain text.
Developer
steve8820
Posts: 75
Registered: ‎04-04-2013
My Device: BB10 Dev Alpha Simulator
My Carrier: Blackberry

Re: Copy current screen image to file

Yup. Had Shared Files checked and added lscreen on pro file. Im testing on Z10 device with version 10.0.10.261. Below is my code.

 

Screenshot.cpp

// Default empty project template
#include "Screenshot.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Window>

#include <screen/screen.h>

#include <qdebug.h>

using namespace bb::cascades;

Screenshot::Screenshot(bb::cascades::Application *app) :
    QObject(app)
{

}

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));
}

bool Screenshot::capture(int width, int height, QString filename)
{
	bool captured = false;

	try {
		screen_pixmap_t screen_pix;
		screen_buffer_t screenshot_buf;
		char *screenshot_ptr = NULL;
		int screenshot_stride = 0;
		screen_context_t context;
		screen_create_context(&context, 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(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");
		QFile outFile("shared/photos/"+ filename +".jpeg");
		outFile.open(QIODevice::WriteOnly);
		image.save(&outFile, "JPEG");

		screen_destroy_pixmap(screen_pix);

		captured = true;
	} catch(std::exception &e) {
		captured = false;
	}

	return captured;
}

 

Screenshot.hpp

// Default empty project template
#ifndef Screenshot_HPP_
#define Screenshot_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class Screenshot : public QObject
{
    Q_OBJECT
public:
    Screenshot(bb::cascades::Application *app);
    //Screenshot(QObject* parent = 0);
    //virtual ~Screenshot() {}

    Q_INVOKABLE bool capture(int width, int height, QString filename);
};

#endif

 

QML button onclicked

Button {
                    id: saveReceipt
                    text: qsTr("Save to Photo Album")
                    onClicked: {
                        _screenShot.capture(768, 1280, lblReceipt.text);
                    }
                }

 

Please advice. Thank you.

Regards,

Steve Chan
Please use plain text.
Developer
steve8820
Posts: 75
Registered: ‎04-04-2013
My Device: BB10 Dev Alpha Simulator
My Carrier: Blackberry

Re: Copy current screen image to file

Hi Steven,

 

Any updates there? I tried everything and seems it just doesnt able to get the screenshot and onlt able to get the image stored to the folder. Do you have any samples i can refer to?

 

Thank you.

Regards,

Steve Chan
Please use plain text.
Developer
slashkyle
Posts: 798
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Copy current screen image to file

[ Edited ]

did you try opening it?  & also, are you on a device or simulator

 

just had to ask because i just took a screen shot using the system method (2 side buttons) and it's 0kb but the image is still there only it registers so small because what i was capturing was mostly made of a few darker colors. 

 

Here's a working sample on Z10 & Dev Alpha-C

 

Saved images are stored in shared/photo's, it requests the shared files permission because I used QSettings to save the captureCount so that it doesnt overwrite each image

Please use plain text.
Developer
slashkyle
Posts: 798
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: Copy current screen image to file

I noticed in the last exaple if the screen was rotated it cropped the image so i have updated the sample to change the capture resolution in qml when the orientation is changed

 

 

Please use plain text.
Developer
steve8820
Posts: 75
Registered: ‎04-04-2013
My Device: BB10 Dev Alpha Simulator
My Carrier: Blackberry

Re: Copy current screen image to file

Thanks slashkyle, I tried opening it and it doesnt open. Tried opening on pc as well and it says the picture is empty. Im testing on Z10 device. Anyway, I will try your sample and give a feed back here. Thanks for your effort for helping. Appreciate it.
Regards,

Steve Chan
Please use plain text.
Developer
steve8820
Posts: 75
Registered: ‎04-04-2013
My Device: BB10 Dev Alpha Simulator
My Carrier: Blackberry

Re: Copy current screen image to file

Hi slashkyle,

 

I found the problem was actually here.

void write_bitmap_header(int nbytes, QByteArray ba, const int size[])

 and replaced it with this

void write_bitmap_header(int nbytes, QByteArray& ba, const int size[])

 Everything works after that... I discovered this when i test the sample you given. Thanks a lot!

Regards,

Steve Chan
Please use plain text.