qt之gif文件提取帧图像

QQ:609162385
gif提取帧
这里写图片描述


#include "alt_key.hpp"
#include "aqp.hpp"
#include "jingleaction.hpp"
#include "mainwindow.hpp"
#include <QApplication>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QImageWriter>
#include <QLabel>
#include <QList>
#include <QMenu>
#include <QMenuBar>
#include <QMovie>
#include <QStatusBar>
#include <QToolBar>


const int StatusTimeout = AQP::MSecPerSecond * 5;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), movieState(NoMovie)
{
    movie = new QMovie(this);

    createActions();
    createMenusAndToolBar();
    createWidgets();
    createLayout();
    createConnections();

    AQP::accelerateMenu(menuBar());
    updateUi();
    statusBar()->showMessage(tr("Open a Movie file to start..."),
                             StatusTimeout);
    setWindowTitle(QApplication::applicationName());
}


void MainWindow::createActions()
{
    jinglePath = AQP::applicationPathOf("jingles");
    imagePath = AQP::applicationPathOf("images");

    fileOpenAction = new JingleAction(
            jinglePath + "/fileopen.wav",
            QIcon(imagePath + "/fileopen.png"), tr("Open..."), this);
    fileOpenAction->setShortcuts(QKeySequence::Open);
    fileSaveAction = new JingleAction(
            jinglePath + "/filesave.wav",
            QIcon(imagePath + "/filesave.png"), tr("Save"), this);
    fileSaveAction->setShortcuts(QKeySequence::Save);
    fileSaveAction->setEnabled(false);
    fileQuitAction = new QAction(
            QIcon(imagePath + "/filequit.png"), tr("Quit"), this);
    fileMuteJinglesAction = new JingleAction(
            jinglePath + "/filemutejingles.wav",
            QIcon(imagePath + "/filemutejingles.png"),
            tr("Mute Jingles"), this);
    fileMuteJinglesAction->setCheckable(true);
    fileMuteJinglesAction->setChecked(false);
#if QT_VERSION >= 0x040600
    fileQuitAction->setShortcuts(QKeySequence::Quit);
#else
    fileQuitAction->setShortcut(QKeySequence("Ctrl+Q"));
#endif

    startOrStopAction = new JingleAction(this);
    takeSnapshotAction = new JingleAction(
            jinglePath + "/takesnapshot.wav",
            QIcon(imagePath + "/takesnapshot.png"),
            tr("Take Snapshot"), this);
}


void MainWindow::createMenusAndToolBar()
{
    setUnifiedTitleAndToolBarOnMac(true);

    QMenu *fileMenu = menuBar()->addMenu(tr("File"));
    QToolBar *fileToolBar = addToolBar(tr("File"));
#ifdef Q_WS_MAC
    fileToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
#endif
    JingleAction *emptyAction = 0;
    foreach (JingleAction *jingleAction, QList<JingleAction*>()
            << fileOpenAction << fileSaveAction << emptyAction
            << fileMuteJinglesAction) {
        if (jingleAction == emptyAction) {
            fileMenu->addSeparator();
            fileToolBar->addSeparator();
            continue;
        }
        fileMenu->addAction(jingleAction);
        fileToolBar->addAction(jingleAction);
    }
    fileMenu->addSeparator();
    fileMenu->addAction(fileQuitAction);

    QMenu *editMenu = menuBar()->addMenu(tr("Edit"));
    QToolBar *editToolBar = addToolBar(tr("Edit"));
#ifdef Q_WS_MAC
    editToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
#endif
    foreach (JingleAction *jingleAction, QList<JingleAction*>()
            << startOrStopAction << takeSnapshotAction) {
        editMenu->addAction(jingleAction);
        editToolBar->addAction(jingleAction);
    }
}


void MainWindow::createWidgets()
{
    movieLabel = new QLabel;
    movieLabel->setMovie(movie);
    movieLabel->setAlignment(Qt::AlignCenter);
    movieLabel->setAutoFillBackground(true);
    movieLabel->setBackgroundRole(QPalette::Dark);
    snapshotLabel = new QLabel;
    snapshotLabel->setAlignment(Qt::AlignCenter);
    snapshotLabel->setAutoFillBackground(true);
    snapshotLabel->setBackgroundRole(QPalette::Dark);
}


void MainWindow::createLayout()
{
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(movieLabel);
    layout->addWidget(snapshotLabel);
    QWidget *widget = new QWidget;
    widget->setLayout(layout);
    setCentralWidget(widget);
}


void MainWindow::createConnections()
{
    connect(fileOpenAction, SIGNAL(triggered()),
            this, SLOT(fileOpen()));
    connect(fileSaveAction, SIGNAL(triggered()),
            this, SLOT(fileSave()));
    connect(fileMuteJinglesAction, SIGNAL(toggled(bool)),
            this, SLOT(muteJingles(bool)));
    connect(fileQuitAction, SIGNAL(triggered()), this, SLOT(close()));
    connect(startOrStopAction, SIGNAL(triggered()),
            this, SLOT(startOrStop()));
    connect(takeSnapshotAction, SIGNAL(triggered()),
            this, SLOT(takeSnapshot()));
}


void MainWindow::updateUi()
{
    if (movieState == Playing) {
        startOrStopAction->setText(tr("&Stop"));
        startOrStopAction->setIcon(QIcon(imagePath +
                                         "/editstop.png"));
        startOrStopAction->setJingleFile(jinglePath +
                                         "/editstop.wav");
    }
    else {
        startOrStopAction->setText(tr("&Start"));
        startOrStopAction->setIcon(QIcon(imagePath +
                                         "/editstart.png"));
        startOrStopAction->setJingleFile(jinglePath +
                                         "/editstart.wav");
    }
    startOrStopAction->setEnabled(movieState != NoMovie);
    takeSnapshotAction->setEnabled(movieState != NoMovie);
}


void MainWindow::fileOpen()
{
    QString fileFormats = AQP::filenameFilter(tr("Movies"),
            QMovie::supportedFormats());
    QString path(movie && !movie->fileName().isEmpty()
            ? QFileInfo(movie->fileName()).absolutePath() : ".");
//    QString filename = QFileDialog::getOpenFileName(this,
//            tr("mp4")
//            .arg(QApplication::applicationName()), path, fileFormats);
    QString filename = QFileDialog::getOpenFileName(this,
          tr("Open Image"), ""
                            "", tr("Image Files (*.mp4 *.gif)"));
    if (filename.isEmpty())
        return;

    movie->setFileName(filename);
    statusBar()->showMessage(tr("Loaded %1").arg(filename),
                             StatusTimeout);
    movieState = Stopped;
    startOrStop(DontReload);
}


void MainWindow::fileSave()
{
    if (snapshot.isNull())
        return;
    QString fileFormats = AQP::filenameFilter(tr("Images"),
            QImageWriter::supportedImageFormats());
    QString filename = QFileDialog::getSaveFileName(this,
            tr("%1 - Save Snapshot")
            .arg(QApplication::applicationName()),
            QFileInfo(movie->fileName()).absolutePath(), fileFormats);
    if (filename.isEmpty())
        return;

    if (!snapshot.save(filename))
        AQP::warning(this, tr("Error"),
                     tr("Failed to save snapshot image"));
    else
        statusBar()->showMessage(tr("Saved %1").arg(filename),
                                 StatusTimeout);
}


void MainWindow::startOrStop(ReloadMode reloadMode)
{
    if (movieState == Stopped) {
        if (reloadMode == Reload)
            movie->setFileName(movie->fileName());
        movie->start();
        movieState = Playing;
    }
    else {
        movie->stop();
        movieState = Stopped;
    }
    updateUi();
}


void MainWindow::takeSnapshot()
{
    snapshot = movie->currentPixmap();
    fileSaveAction->setEnabled(!snapshot.isNull());
    snapshotLabel->setPixmap(snapshot);
}


void MainWindow::muteJingles(bool mute)
{
    JingleAction::setMute(mute);
}
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>
#include <QPixmap>


class JingleAction;
class QLabel;
class QMovie;


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    enum ReloadMode {DontReload, Reload};

    explicit MainWindow(QWidget *parent=0);

private slots:
    void fileOpen();
    void fileSave();
    void startOrStop(ReloadMode reloadMode=Reload);
    void takeSnapshot();
    void muteJingles(bool mute);
    void updateUi();

private:
    enum MovieState {NoMovie, Stopped, Playing};

    void createActions();
    void createMenusAndToolBar();
    void createWidgets();
    void createLayout();
    void createConnections();

    JingleAction *fileOpenAction;
    JingleAction *fileSaveAction;
    JingleAction *fileMuteJinglesAction;
    JingleAction *startOrStopAction;
    JingleAction *takeSnapshotAction;
    MovieState movieState;
    QAction *fileQuitAction;
    QLabel *movieLabel;
    QMovie *movie;
    QLabel *snapshotLabel;
    QPixmap snapshot;
    QString jinglePath;
    QString imagePath;
};

#endif // MAINWINDOW_HPP

猜你喜欢

转载自blog.csdn.net/cqltbe131421/article/details/81327111
今日推荐