Ted
Ted

Reputation: 99

Problem with QT save() function for QImage

I have a problem which I have been trying to figure out for few days now. I am using OpenCV to get frames from a camera and then I am converting it to QImage and trying to save the frame on a disk. The problem I am facing is that if I compile/run the code in release/debug mode in visual studio then it works fine. If I run the executable created by visual studio for debug mode then it runs fine. But if I run the executable created by visual studio in release mode, the gui works well except that the QImage save() function doesn't work anymore. If I use OpenCV function for saving the frame then it works well in all modes. The QImage that I am trying to save is notNull because my comments text box displays the line I print inside but the save function returns false. I would really appreciate help.

Here is a little code:

Code for conversion between IplImage and QImage (I have also used another code, with the same result)

QImage A::IplImage2QImage(const IplImage *iplImage)
{
    int height = iplImage->height;
    int width = iplImage->width; 
    if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3) {
        const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
        QImage img(qImageBuffer, width, height, QImage::Format_RGB888); 
        return img.rgbSwapped();
    }
    else {
        // qWarning() << "Image cannot be converted.";  
        return QImage();
    }
}

void A::getFrame()
{
    commentsTextBox->setText("getFrame");
    if (VI.isFrameNew(device1)) {
        VI.getPixels(device1, (unsigned char *)frame->imageData, false, true);
    }
    QImage qimg2 = IplImage2QImage(frame);
    m_Label->setPixmap(QPixmap::fromImage(qimg2));
    if (!qimg2.isNull()) {
        x = qimg2.save("C:\\Data\\test.jpg");
        commentsTextBox->append("notEmpty33"); 
    }
}

Upvotes: 4

Views: 7423

Answers (2)

Sree
Sree

Reputation: 1

Try something like this,

// Prepare the url,
QUrl url("file:///home/ubuntu/Desktop/image.jpg");

// Get the path,
QString path = url.path();

// Save the image,
myQimage.save(path);

Upvotes: 0

dehumanizer
dehumanizer

Reputation: 41

I had the same problem to QImage::load(). I will post a copy of the solution pointed out in the link of alexisdm, in order to make it clearer.

Under windows the qjpeg4.dll must in a imageformats directory under the application's directory by default.

Thank you!

Upvotes: 4

Related Questions