Qt Open Source Works 11-Screen Recording Control

1. Preface

In the normal writing process, it is often necessary to capture some action and effect pictures in GIF format to make the information covered more comprehensive and vivid. Sometimes you can record the entire operation process and operation effect as MP4, but the file size is large, and many websites are not easy to upload. Basically, GIF motion pictures are supported, generally with a resolution of about 5 seconds gif, 800*600, can be well controlled within 500 KB, so that it will be more perfect to support the drive map on major websites. Beginning with ScreenGif.exe, after a long time, it feels OK, recommended by a friend in the future LICEcap.exe, smaller, higher compression ratio, and later found that there was aGif.hOpen source class, call one of the methods to merge multiple pictures into one gif, but also cross-platform, I personally tested successfully in WIN+UBUNTU. The original code was given by Nie Master, on which I refined it so that you can drag the size of the form directly to change the size of the recording area.Added support for Qt4 and other compilers.

Main functions:

  1. The width and height of the screen to be recorded can be set to support direct pull change in the lower right corner.
  2. Width can be set
  3. Sets the background color of the recording control
  4. Setable number of frames recorded
  5. Recording area can be freely dragged to select

2. Code ideas

void GifWidget::saveImage()
{
    if (!gifWriter) {
        return;
    }

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
    //Since qt4 does not have RGBA8888, ARGB32, which is closest to RGBA8888, has a slight color bias
    QPixmap pix = QPixmap::grabWindow(0, x() + rectGif.x(), y() + rectGif.y(), rectGif.width(), rectGif.height());
    QImage image = pix.toImage().convertToFormat(QImage::Format_ARGB32);
#else
    QScreen *screen = QApplication::primaryScreen();
    QPixmap pix = screen->grabWindow(0, x() + rectGif.x(), y() + rectGif.y(), rectGif.width(), rectGif.height());
    QImage image = pix.toImage().convertToFormat(QImage::Format_RGBA8888);
#endif

    gif.GifWriteFrame(gifWriter, image.bits(), rectGif.width(), rectGif.height(), fps);
    count++;
    labStatus->setText(QString("Recording No. %1 frame").arg(count));
}

void GifWidget::record()
{
    if (btnStart->text() == "start") {
        if (0 != gifWriter) {
            delete gifWriter;
            gifWriter = 0;
        }

        //First pop up the file save dialog box
        //fileName = qApp->applicationDirPath() + "/" + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss.gif");
        fileName = QFileDialog::getSaveFileName(this, "Select a Save Location", qApp->applicationDirPath() + "/", "gif picture(*.gif)");
        if (fileName.isEmpty()) {
            return;
        }

        int width = txtWidth->text().toInt();
        int height = txtHeight->text().toInt();
        fps = txtFps->text().toInt();

        gifWriter = new Gif::GifWriter;
        bool bOk = gif.GifBegin(gifWriter, fileName.toLocal8Bit().data(), width, height, fps);
        if (!bOk) {
            delete gifWriter;
            gifWriter = 0;
            return;
        }

        count = 0;
        labStatus->setText("start recording...");
        btnStart->setText("Stop it");
        //Delayed start
        timer->setInterval(1000 / fps);
        QTimer::singleShot(1000, timer, SLOT(start()));
        //saveImage();
    } else {
        timer->stop();
        gif.GifEnd(gifWriter);

        delete gifWriter;
        gifWriter = 0;

        labStatus->setText(QString("Recording Completed %1 frame").arg(count));
        btnStart->setText("start");
        QDesktopServices::openUrl(QUrl(fileName));
    }
}

3. Effect Charts

4. Open Source Home Page

The complete source downloads of the above works are all on the open source homepage. The quantity and quality of the works will be continuously updated, and you are welcome to pay attention.

  1. Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
  2. International site: https://github.com/feiyangqingyun/QWidgetDemo
  3. Personal home page: https://blog.csdn.net/feiyangqingyun
  4. Knowing Home Page: https://www.zhihu.com/people/feiyangqingyun/

Tags: Ubuntu github

Posted on Fri, 15 May 2020 22:11:48 -0400 by neogemima