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

折月煮酒 提交于 2020-04-30 04:44:45

问题


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?


回答1:


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());
}


来源:https://stackoverflow.com/questions/33955895/create-video-file-using-sequence-of-qpixmap-in-qt-creator-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!