日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

qt中实现左右分割线_Qt项目中,实现屏幕截图并生成gif的详细示例(值得细读)...

發(fā)布時(shí)間:2023/12/2 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qt中实现左右分割线_Qt项目中,实现屏幕截图并生成gif的详细示例(值得细读)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

總第50篇

平時(shí)我們?cè)诠ぷ骱蛯W(xué)習(xí)的過(guò)程中,有時(shí)需要將桌面的某些動(dòng)作截圖生成gif動(dòng)圖,以更生動(dòng)地呈現(xiàn)出來(lái)。目前有很多這樣的軟件,并且方便易使用,比如我經(jīng)常使用的GifCam,軟件小巧,生成的圖片文件也比較小,非常優(yōu)秀。它的界面如下圖所示(這里絕不是打廣告呀)。

那么,這種截圖后生成gif文件是如何用軟件實(shí)現(xiàn)的呢?

本文將詳細(xì)說(shuō)明一下這種功能的實(shí)現(xiàn)思路,作為軟件設(shè)計(jì)的一種參考,也方便在以后的工程項(xiàng)目中借鑒與參照。

1.多張image圖片處理成gif圖片的實(shí)現(xiàn)

這個(gè)可以借助gif開源的類來(lái)實(shí)現(xiàn),直接調(diào)用其中的方法,可以實(shí)現(xiàn)將多張圖片合并到一張gif圖片中去,并且這個(gè)類還是跨平臺(tái)的,方便在多平臺(tái)上實(shí)現(xiàn)。這里列舉幾個(gè)要用到的接口的實(shí)現(xiàn),詳細(xì)的接口實(shí)現(xiàn)請(qǐng)查看完整的開源類文件(若找不到,可以聯(lián)系我,問(wèn)我要)。

struct GifWriter { //這個(gè)是寫gif的結(jié)構(gòu)體定義FILE *f;uint8_t *oldImage;bool firstFrame;}; //開始生成gif文件的接口bool GifBegin( GifWriter *writer, const char *filename,uint32_t width, uint32_t height,uint32_t delay, int32_t bitDepth = 8,bool dither = false ){(void)bitDepth;(void)dither; // Mute "Unused argument" warnings #if defined(_MSC_VER) && (_MSC_VER >= 1400)writer->f = 0;fopen_s(&writer->f, filename, "wb"); #elsewriter->f = fopen(filename, "wb"); #endifif(!writer->f) {return false;}writer->firstFrame = true;// allocatewriter->oldImage = (uint8_t *)GIF_MALLOC(width * height * 4);fputs("GIF89a", writer->f);// screen descriptorfputc(width & 0xff, writer->f);fputc((width >> 8) & 0xff, writer->f);fputc(height & 0xff, writer->f);fputc((height >> 8) & 0xff, writer->f);fputc(0xf0, writer->f); // there is an unsorted global color table of 2 entriesfputc(0, writer->f); // background colorfputc(0, writer->f); // pixels are square (we need to specify this because it's 1989)// now the "global" palette (really just a dummy palette)// color 0: blackfputc(0, writer->f);fputc(0, writer->f);fputc(0, writer->f);// color 1: also blackfputc(0, writer->f);fputc(0, writer->f);fputc(0, writer->f);if( delay != 0 ) {// animation headerfputc(0x21, writer->f); // extensionfputc(0xff, writer->f); // application specificfputc(11, writer->f); // length 11fputs("NETSCAPE2.0", writer->f); // yes, reallyfputc(3, writer->f); // 3 bytes of NETSCAPE2.0 datafputc(1, writer->f); // JUST BECAUSEfputc(0, writer->f); // loop infinitely (byte 0)fputc(0, writer->f); // loop infinitely (byte 1)fputc(0, writer->f); // block terminator}return true;}//向gif文件生成過(guò)程中寫入一幀數(shù)據(jù)的接口bool GifWriteFrame( GifWriter *writer, const uint8_t *image,uint32_t width, uint32_t height,uint32_t delay, int bitDepth = 8, bool dither = false ){if(!writer->f) {return false;}const uint8_t *oldImage = writer->firstFrame ? NULL : writer->oldImage;writer->firstFrame = false;GifPalette pal;GifMakePalette((dither ? NULL : oldImage), image, width, height, bitDepth, dither, &pal);if(dither) {GifDitherImage(oldImage, image, writer->oldImage, width, height, &pal);} else {GifThresholdImage(oldImage, image, writer->oldImage, width, height, &pal);}GifWriteLzwImage(writer->f, writer->oldImage, 0, 0, width, height, delay, &pal);return true;}

2.定時(shí)生成圖片并記錄圖片

用定時(shí)器的singleShot()定時(shí)截取桌面的的一幀幀圖片,將圖片保存處理后送入到gif的GifWriteFrame() 接口,生成相應(yīng)的動(dòng)圖。這里關(guān)鍵在于從桌面上獲取截圖,要用到grabWindow()這個(gè)接口函數(shù)。

對(duì)于Qt5以前和版本, 這個(gè)接口函數(shù)是放在QPixmap中,獲取的圖片也是QPixmap格式的。對(duì)于Qt5以上的版本,這個(gè)接口是單獨(dú)放進(jìn)了一個(gè)類,這個(gè)類叫QScreen,獲取的圖片也是QPixmap格式的。

其關(guān)鍵的兩個(gè)函數(shù)代碼示例如下:

//從桌面截圖并幀寫入到gif中 void GifWidget::saveImage() {if (!gifWriter) {return;}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);gif.GifWriteFrame(gifWriter, image.bits(), rectGif.width(), rectGif.height(), fps); } //點(diǎn)擊開始錄制時(shí),打開定時(shí)器觸發(fā)槽函數(shù), void GifWidget::record() {if (btnStart->text() == "開始") {if (0 != gifWriter) {delete gifWriter;gifWriter = 0;}//確定gif文件的保存位置fileName = QFileDialog::getSaveFileName(this, "選擇保存位置", qApp->applicationDirPath() + "/", "gif圖片(*.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("開始錄制...");btnStart->setText("停止");//延時(shí)啟動(dòng)timer->setInterval(1000 / fps);QTimer::singleShot(1000, timer, SLOT(start()));} else {timer->stop();gif.GifEnd(gifWriter);delete gifWriter;gifWriter = 0;labStatus->setText(QString("錄制完成 共 %1 幀").arg(count));btnStart->setText("開始");QDesktopServices::openUrl(QUrl(fileName));} }

其程序運(yùn)行的結(jié)果如下圖所示:

總起來(lái)說(shuō),整個(gè)功能的實(shí)現(xiàn)分為兩步,第一是定時(shí)從桌面獲取截圖圖片,第二是將這些圖片按幀組合成gif文件。 程序的實(shí)現(xiàn)只是一種參考,希望對(duì)你有參考意義。

本文到此結(jié)束!

如果對(duì)你有幫助,請(qǐng)隨手 點(diǎn)贊 或 點(diǎn)喜歡!關(guān)注本專欄,更多干貨與你分享。

=======================================================

歡迎【關(guān)注、私信 @武三郎】。我們一起交流一起進(jìn)步。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的qt中实现左右分割线_Qt项目中,实现屏幕截图并生成gif的详细示例(值得细读)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。