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
Contributor
AleksDef
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer
Accepted Solution

ImageView and Qimage integration

[ Edited ]

Much of this issue has been discussed here:

http://supportforums.blackberry.com/t5/Cascades-Development/ImageView-cascades-noob-question/td-p/17...

 

Basically, a suggested correct way of converting a QImage to ImageView is this one:

 

PixelBufferData pbd (PixelBufferData::RGBA_PRE,yourQImage.width(),yourQImage.height(),yourQImage.width(),yourQImage.bits());

yourImageView->setImage(pbd);

 

Then there was someone specifying that the format should probably be BGR instead of RGB, which could be done with OpenGL or like this:

 

image.rgbSwapped();

Here is my code I have a problem with:

PixelBufferData pbd(PixelBufferData::RGBA_PRE, orgPattern.width(), orgPattern.height(),  orgPattern.width(),  orgPattern.rgbSwapped().bits());
m_patternFrame->setImage(pbd);

orgPattern is a QImage loaded from a file path that is certain to exist. m_patternFrame is a pointer to ImageView.

I know that m_patternFrame is a valid pointer because if I call its method setImageSource with the path I created the QImage from, it displays that picture. But when I run that code, this is printed in the console:

Context: Failed to find target node with id %d 906.

 

Some times the number is different. When I tested again, it was 563. The application doesn't cursh or freeze, but nothing happens with the frame, it's still blank.

 

Can anyone provide any help with this issue?

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ImageView and Qimage integration

Thank you for starting a new thread and referencing another thread.  That helps us give you a timely response.

 

Before we delve into this too far, what is the width and height?  (We are within bounds, I assume?)

 

And can you try doing the setImage in 2 steps: first create the Image from the pdb rather than implicitly, and test for !image.isNull()

 

Stuart

Please use plain text.
Contributor
AleksDef
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: ImageView and Qimage integration

[ Edited ]

Thanks for the quick reply. You were right, the image initially was null and that was generating the context issue.

 

However, now I have a potentially deeper issue. After I fixed the image so it is correct (and verified this by exporting it and checking), I found out that now it freezes the application if I try to set the buffer to the frame, or create a bb::cascades::Image from it. Here is what I tried (and it made no change, still freezes):

- dynamic allocation of QImage

- RGBX instead of RGBA_PRE; the QImage was created with either QImage::Format_RGB32 for the first type, or QImage::Format_ARGB32_Premultiplied for the second type.

- removed rgbSwapped();

- changed width() with bytesPerLine() on the fourth argument

 

This is the creation code:

 

QImage currentPattern(600, 600, QImage::Format_RGB32);
currentPattern.fill(QColor::fromRgb(127,127,127));

QPainter painter;
painter.begin(&currentPattern);
//...
//Some calls to painter.drawImage removed for brevity
//...
painter.end();

PixelBufferData pbd(PixelBufferData::RGBX, currentPattern.width(), currentPattern.height(), currentPattern.width(), currentPattern.rgbSwapped().bits());
printf("Buffer created!\n");
fflush(stdout);
bb::cascades::Image img(pbd);
printf("Image created!\n");
fflush(stdout);
m_patternFrame->setImage(img);
printf("Function has finished!\n");
fflush(stdout);

Basically, it never gets to print out: "Image created!"

m_patternFrame is a pointer to ImageView which has a blank QUrl set as a source, i.e. it is empty so far. There is a workaround around this whole buffer thing, (which is exporting the image and reading it through setImageSource), but that workaround is performance kill if I need to use this frequently.

 

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ImageView and Qimage integration

[ Edited ]

I'm going to play with this, but in the meantime, please see:

http://supportforums.blackberry.com/t5/Cascades-Development/Using-QPainter-on-QImage/m-p/1750877/hig...

 

Restrict yourself to Qt classes documented in:

https://developer.blackberry.com/cascades/reference/qt_index.html

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: ImageView and Qimage integration

[ Edited ]

I was hoping this would lead to a workaround for what your are trying to do in R4.

Please take a look at:

https://www.blackberry.com/jira/browse/BBTEN-1

 

If this affects you, please vote for it.

 

I don't see an issue directly on image manipulation.  If you feel you need additional features, please request a feature for it in JIRA.  The only workaround I see at the moment is to use routines to manipulate the underlying image data.

 

Stuart

 

Please use plain text.
Contributor
AleksDef
Posts: 42
Registered: ‎05-30-2012
My Device: Simulator
My Carrier: Developer

Re: ImageView and Qimage integration

[ Edited ]

Hey, thanks for the quick replies. It just comes up that I managed to "work-around" this issue somehow. I saw in the topic you suggested that QPainter isn't fully supported, though it seems to work fine for me.

 

I'll post the actual solution now. Thing is, I had to copy image buffer data on memory I allocated dynamically. Then I used this buffer to create PixelBufferData. Even though it is slow, it is probably an effective workaround until the image creation is fixed and can be done safely from QImage::bits(). Here is the code:

 

QImage swapped = m_displayCopy.rgbSwapped();
const uchar* pointer = swapped.constBits();

int pixels_total = swapped.width() * swapped.height() * 4;
unsigned int* buffer = (unsigned int*)malloc(pixels_total);
memset(buffer, 0xff, pixels_total);	//Set all to white first
memcpy(buffer, pointer, qMin(pixels_total, swapped.byteCount())); //The qMin is some sort of a safety check
PixelBufferData pbd(PixelBufferData::RGBX, swapped.width(), swapped.height(), swapped.width(), buffer); m_image->setImage(pbd); free(buffer);

And this works. I'll mark your first answer as a solution as it is truly the solution to the first problem.

Please use plain text.