Sandun Harshana
Sandun Harshana

Reputation: 729

Create video file using sequence of QPixmap in QT Creator- C++

I have QWidget (named as screenshotLabel) and continuously it's content is changing.I can get that lable content to qpixmap (named as originalPixmap) as bellow.

originalPixmap = QPixmap(); 
QPixmap pixmap(screenshotLabel->size());
this->render(&pixmap);
originalPixmap = pixmap;

Now I want to save it as a video file.But I could not able to do it.How can I save QWidget content as a video file?

Upvotes: 1

Views: 3235

Answers (1)

Sandun Harshana
Sandun Harshana

Reputation: 729

I found a way to generate video using OpenCV VideoWriter.I leave comments in the code that describe what is happening.

originalPixmap = pixmap;
qImageSingle = originalPixmap.toImage(); // Convert QPixmap to QImage

// Get QImage data to Open-cv Mat
frame = Mat(qImageSingle.height(), qImageSingle.width(), CV_8UC3, qImageSingle.bits(), qImageSingle.bytesPerLine()).clone();

namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
imshow("MyVideo", frame);

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
try {
    imwrite("alpha2.png", frame, compression_params);
    VideoWriter video("out2.avi", CV_FOURCC('M','J','P','G'), 10, Size(qImageSingle.width(), qImageSingle.height()), true);
    for(int i=0; i<100; i++){
       video.write(frame); // Write frame to VideoWriter
    }
}
catch (runtime_error& ex) {
    fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
}

Upvotes: 7

Related Questions