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
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Saving an Image in C++

How would one go about saving an image using C++ that is currently being view in qml?

I have created a function which I can call from qml and pass the image (bb::casacdes::image) and a filename (QString) to C++, but what code would then allow me to save that image to a new file?

Developer
Posts: 217
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Saving an Image in C++

I have not done this yet. But you can save a file in Qt with the QFile class.

 

QFile file("filename");
file.write(data);

 You have to create a QByteArray containing the image data and pass that to the write() method.

 

You can also try searching the Qt forum and wiki for a solution: http://qt-project.org/doc/qt-4.8/

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Saving an Image in C++

Ya the difficult part is figuring out how to move the image to a QByteArray Smiley Sad

Developer
Posts: 163
Registered: ‎04-28-2012
My Device: BlackBerry 9800
My Carrier: BlackBerry

Re: Saving an Image in C++

Hi,

 

   I think the below code will solve your problem.

 

DownloadImage::DownloadImage(QString imgUrl)
{
	imgPath=imgUrl;
	QStringList filePathList = imgPath.split('/');
	QString bgimage = filePathList.at(filePathList.count() - 1);
	imagePath =home+"/" + bgimage;
	QNetworkAccessManager *imgManager=new QNetworkAccessManager();
	QObject::connect(imgManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(downloadImg(QNetworkReply *)));
	QNetworkRequest request;
	request.setUrl(QUrl(imgPath));
	imgManager->get(request);
}


void DownloadImage::downloadImg(QNetworkReply *imgReply)
{
	if(imgReply->error()==QNetworkReply::NoError)
	{
		imgFile=new QFile(imagePath);
		if(!imgFile->open(QIODevice::WriteOnly))
		{
			qDebug()<<"error writing file";
		}
		else
		{
			imgFile->write(imgReply->readAll());
			imgFile->flush();
			imgFile->close();
		}
	}
	}

 

Regards,

Naresh Kodumuri.

Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Saving an Image in C++

Thanks Naresh,

It looks like the code you posted is taking an Image from a QNetworkReply which is already in QByteArray format.

I have an Image which I'm sending from qml to C++ and I need to convert this image to the QByteArray. Unfortunately, I have been unable to find any way to convert this in the documentation.

Highlighted
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: Saving an Image in C++

This code is used to set an Image from a QByteArray found in a QNetworkReply data:

void DownloadImage::downloadImg(QNetworkReply *imgReply)
{
	setImage(Image(imgReply-readAll));
}

 What I want to do is the exact opposite...Go from an Image to a QByteArray