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
bemipefe
Posts: 75
Registered: ‎06-14-2013
My Device: Z10

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
pyth
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane

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 :smileyhappy:

 

 

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
bemipefe
Posts: 75
Registered: ‎06-14-2013
My Device: Z10

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.