| 32 | namespace pag { |
| 33 | |
| 34 | QImage PAGImageLayerModel::GetVideoFirstFrame(const QString& filePath) { |
| 35 | QMediaPlayer player; |
| 36 | QVideoSink sink; |
| 37 | QEventLoop loop; |
| 38 | QImage result = {}; |
| 39 | |
| 40 | connect(&sink, &QVideoSink::videoFrameChanged, [&](const QVideoFrame& frame) { |
| 41 | QVideoFrame videoFrame(frame); |
| 42 | videoFrame.map(QVideoFrame::ReadOnly); |
| 43 | QImage image = videoFrame.toImage(); |
| 44 | if (!image.isNull()) { |
| 45 | result = image.copy(); |
| 46 | loop.quit(); |
| 47 | } |
| 48 | videoFrame.unmap(); |
| 49 | }); |
| 50 | |
| 51 | player.setVideoSink(&sink); |
| 52 | player.setSource(filePath); |
| 53 | player.setLoops(1); |
| 54 | player.play(); |
| 55 | |
| 56 | QTimer timer; |
| 57 | timer.setSingleShot(true); |
| 58 | connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); |
| 59 | timer.start(1000); |
| 60 | |
| 61 | loop.exec(); |
| 62 | player.stop(); |
| 63 | |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | PAGImageLayerModel::PAGImageLayerModel(QObject* parent) : QAbstractListModel(parent) { |
| 68 | } |