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
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified
Accepted Solution

How to convert the Qbytearray to image in cascades

Hi Guys,

 

How can i convert Qbytearray to image ,so that i can show in imageView.Any sample codes will be useful

 

 

Regards

Rakesh Shankar.P

Contributor
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: How to convert the Qbytearray to image in cascades

[ Edited ]

QImage has a constructor that takes a "uchar* data". I suppose the buffer is storred in QByteArray, so try to use the data in the byte array to construct a QImage using the appropriate constructor. If your QImage is valid, it is simple from now on:

 

QImage swapped = originalImageFromBuffer.rgbSwapped();

PixelBufferData pbd(RGBX /* or RGBA_PRE, depends on the buffer format*/, swapped.width(), swapped.height(), swapped.width(), (void*)swapped.constBits());

myImageView->setImage(pbd);

 

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to convert the Qbytearray to image in cascades

You're in good hands with AleksDef.

Read the full chain of thought at http://supportforums.blackberry.com/t5/Cascades-Development/ImageView-and-Qimage-integration/td-p/17...

 

Stuart

Developer
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to convert the Qbytearray to image in cascades

Hi,

 

Still i am unable to retreive the images from server.Code that i used is

void HelloCascadesApp::finishedSlot(QNetworkReply* reply)
{
    //printf (" %d ","In finishes slot");
     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
     QByteArray data = reply->readAll();
     uchar* imageChar = (uchar*)data.data();
     QImage swapped(imageChar,200,150,QImage::Format_RGB32);

//     PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), (void*)swapped.constBits());
     PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), swapped.rgbSwapped().bits());

     ImageView *testImage = new ImageView();
    // Image imageAsset = Image(QUrl("asset:///images/bubble.png"));

     Image image(pbd);
     testImage->setImage(image);

     container->add(testImage);


     qDebug() << "Status :" << statusCode;
    // imageView->setImage(temp);

}

 besides there is another question of printing in console,i tried using Qdebug of Qt,but i am not able to getting anything in console,and there is anything in cascades to show pop up?

 

Regards

Rakesh Shankar.P

Contributor
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: How to convert the Qbytearray to image in cascades

[ Edited ]

First of all, never use this:

 

PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), swapped.rgbSwapped().bits());

This part: swapped.rgbSwapped().bits() caused issues (at least for me, because from my experience, swapped.width() was somehow not equal to swapped.rgbSwapped().width()).

What you need to do is store image.rgbSwapped() in a separate variable:

 

QImage swapped = imageCreatedFromData.rgbSwapped();

 Then use:

 

PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), (void*)swapped.constBits());

It may look like a waste of space, but it is actually a workaround (was for me).

So your code should be like this:

 

//printf (" %d ","In finishes slot");
     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
     QByteArray data = reply->readAll();
     uchar* imageChar = (uchar*)data.data();
     QImage originalImage(imageChar,200,150,QImage::Format_RGB32);

     QImage swapped = originalImage.rgbSwapped();
     PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), (void*)swapped.constBits());

     ImageView *testImage = new ImageView();

     Image image(pbd);
     testImage->setImage(image);

     container->add(testImage);

     qDebug() << "Status :" << statusCode;

 The qDebug() call probably isn't reached because the application freezes if you use the code you used previously.

So, try this modified code. If this doesn't work, then check for image.isNull(). Maybe try to export the image somewhere and see if it is valid. If it is valid, there is no reason this modified code not to work.

 

Developer
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to convert the Qbytearray to image in cascades

Qdebug is not working in any class,i tried giving qdebug in constructor ,but still am not gettting anything in console.and i am getting the error as segmention fault in the line

 

 

QImage swapped = originalImage.rgbSwapped();

 I tried checking orginalImage.isNull()..i am getting as false,which shows image is not null.But still i am not able to figure the reason for this error.

 

Regards

Rakesh Shankar.P

Contributor
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: How to convert the Qbytearray to image in cascades

[ Edited ]

A segmentation fault is something else entirely since it is generated by QImage directly. I can't help you much with that. However, maybe try exporting or drawing the image somewhere to see if it is really ok, maybe this will shed some light onto your problem.

Perhaps smacmartin can provide insigth with the segfault or with qDebug. Regarding the latter, have you tried doing:

 

printf("message to output");
fflush(stdout);
Developer
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to convert the Qbytearray to image in cascades

Hi,

 

Thanks for inputs .

 

When i  use image of large size like http://www.standrews.k12.nf.ca/Computerwebquest/Computer.JPG,i am geting segmentation fault,similarly when i use small image like http://img-android.lisisoft.com/imgmic/8/0/1708-i-com.july.cricinfo.jpg,I am not getting segmentation fault,but i am getting a image,but the black portion of imageView.RESULT.jpg

 

Similarly for debugging in console ,Console gets blank while app is getting debugged,Is there configuration change for eclipse that needs to be done?

 

IssueEclipse.jpg

 

One more qn,regarding making networking call,i use signals and slots for making the image request call,Is there anyway apart from it?I am not fully aware any other APIs.

 

Regards

Rakesh Shankar.P

Contributor
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: How to convert the Qbytearray to image in cascades

[ Edited ]

The only case I've read a segmentation fault can happen by image editing on BB is if the image is larger than 2000x2000. However, I'm guessing that if you hardcode image dimensions in QImage's constructor, and the buffer you are using is actually bigger than the dimensions, then you might get a segfault. Similarly, if the buffer is smaller than the given width and height, perhaps this might cause black (empty) areas in the generated QImage. Not really sure on this, though.

 

For debugging I've always used printf with fflush or qDebug in QNX Momentics. This has always worked with Project > Run As. I don't know what happenes with Project > Debug As, never could've gotten that to work.

 

I don't know about the last question, maybe this is what you are looking for: https://developer.blackberry.com/cascades/reference/bb__cascades__webloadrequest.html. If not, open up a new question about this.

Developer
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to convert the Qbytearray to image in cascades

[ Edited ]

Thanks for your help ,i am was able to retreive image

 QImage swapped;
	 qDebug()<<"data"<<data.length();
	 swapped.loadFromData(data,0);
	 qDebug()<<"swapped"<<swapped.width();
	 QImage assignedImage = swapped.rgbSwapped();
	 PixelBufferData pbd(PixelBufferData::RGBX, assignedImage.width(), assignedImage.height(), assignedImage.width(), (void*)assignedImage.constBits());
 ImageView *testImage = new ImageView();
     Image image(pbd);
     testImage->setImage(image);
     container->add(testImage);

 Regards

Rakesh Shankar.P