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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Regular Contributor
Posts: 76
Registered: ‎06-14-2013
My Device: Z10
My Carrier: PosteMobile

ImageButton problem: Image not showed when image created from QImage

I need to open a PNG file in the assets, crop it using QImage and then converting it back to an Image to display it. I managed to do that by using 2 functions I made for the purpose. The code seem to woork indeed when I put the image generated with cropImage() function in an ImageView I can see the image correctly displayed.

However when I put the image in a ImageButton by setting it with setDefaultImage() the image is not displayed at all. I think the problem is related to the missing source URL of the image. The modified image of course doesn't exsist on the file system and I can't save each modified image to the filesystem.  

 

Here is the code:

 

Image * ImageUtils::cropImage(Image *OriginalImage , int new_width , int new_height)
{
	bool result;
	int length;
	QString OriginalImagePath;
	QByteArray OriginalImageContent;
	QImage BufferedImage;
	QImage CroppedImage;
	QFile *OriginalImageFile;
	bb::ImageData ResultImage;
	unsigned char *RawImageData;

	OriginalImagePath = OriginalImage->source().toString();
	OriginalImagePath.remove("asset://");
	OriginalImagePath = QDir::currentPath() + "/app/native/assets" + OriginalImagePath;

	Logger::writeLog("Resizing Image: %s\n" , OriginalImagePath.toAscii().data() );

	OriginalImageFile = new QFile(OriginalImagePath.toAscii().data() );
	OriginalImageFile->open(QFile::ReadOnly);
	OriginalImageContent = OriginalImageFile->readAll();

	length = OriginalImageContent.length();
	Logger::writeLog("Image size: %d\n" , &length);

	result = BufferedImage.loadFromData(OriginalImageContent);
	OriginalImageFile->close();
	delete OriginalImageFile;

	if(result == false) return NULL;

	Logger::writeLog("Image loaded\n");

	CroppedImage = BufferedImage.copy(0 , 0 , new_width , new_height);
	Logger::writeLog("Image cropped\n");

	RawImageData = convertQImageToRawData(&CroppedImage , bb::PixelFormat::RGBA_Premultiplied);

	Logger::writeLog("Image converted to raw data\n");

	return new Image(ResultImage.fromPixels(RawImageData , bb::PixelFormat::RGBA_Premultiplied , CroppedImage.width() , CroppedImage.height() , CroppedImage.width() * 4) );
}

unsigned char * ImageUtils::convertQImageToRawData(QImage *ImageObject , bb::PixelFormat::Type pixel_format)
{
    int x;
    int y;
    int width;
    int height;

    QRgb single_pixel;

    width = ImageObject->width();
    height = ImageObject->height();

    Logger::writeLog("Image width: %d\n" , &width);
    Logger::writeLog("Image height: %d\n" , &height);

    unsigned char *raw_data;

    if(ImageObject == NULL) return NULL;

    raw_data = new unsigned char[width * height * 4];

    memset(raw_data , 0 , width * height * 4);

    if(pixel_format == bb::PixelFormat::RGBX)
    {
        for(y = 0 ; y < height ; y++)
        {
            for(x = 0 ; x < width ; x++)
            {
                single_pixel = ImageObject->pixel(x , y);

                (raw_data + (y * width * 4) )[x * 4] = qRed(single_pixel);
                (raw_data + (y * width * 4) )[(x * 4) + 1] = qGreen(single_pixel);
                (raw_data + (y * width * 4) )[(x * 4) + 2] = qBlue(single_pixel);
                (raw_data + (y * width * 4) )[(x * 4) + 3] = qAlpha(single_pixel);
            }
        }
    }

    if(pixel_format == bb::PixelFormat::RGBA_Premultiplied)
    {
        for(y = 0 ; y < height ; y++)
        {
            for(x = 0 ; x < width ; x++)
            {
                single_pixel = ImageObject->pixel(x , y);

                (raw_data + (y * width * 4) )[x * 4] = (qRed(single_pixel) * qAlpha(single_pixel) ) >> 8 ;
                (raw_data + (y * width * 4) )[(x * 4) + 1] = (qGreen(single_pixel) * qAlpha(single_pixel) ) >> 8;
                (raw_data + (y * width * 4) )[(x * 4) + 2] = (qBlue(single_pixel) * qAlpha(single_pixel) ) >> 8;
                (raw_data + (y * width * 4) )[(x * 4) + 3] = qAlpha(single_pixel);
            }
        }
    }

    return raw_data;
}

 

Any help is appreciated.

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: ImageButton problem: Image not showed when image created from QImage

I don't have the time to look into your code, but a simple workaround would be a Container with an ImageView underneath and a button on top Smiley Happy

 

 

Container {

            layout: DockLayout {

            }

            ImageView {
                id: buttonImageView
                preferredWidth: 100
                preferredHeight: 100
                imageSource: //<--your cropped image
            }

            Button {
                preferredWidth: buttonImageView.preferredWidth
                preferredHeight: buttonImageView.preferredHeight
                onClicked: {
                    //do stuff
                }
                opacity: 0.1
            }
        }

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Regular Contributor
Posts: 76
Registered: ‎06-14-2013
My Device: Z10
My Carrier: PosteMobile

Re: ImageButton problem: Image not showed when image created from QImage

I would prefer to switch from ImageButton to ImageView and connect to clicked() signal of the ImageView. It's useless to overlap an ImageButton if the image it's not displayed.

 

Thank you.