qt用多线程实现GIF转字符动画代码项目

时间: 2024-02-16 09:04:10 浏览: 29
以下是一个简单的 Qt 多线程实现 GIF 转字符动画的示例代码: ```cpp #include <QCoreApplication> #include <QImage> #include <QPainter> #include <QThread> #include <QMutex> #include <QDebug> class GifToAsciiWorker : public QObject { Q_OBJECT public: GifToAsciiWorker(const QString& filePath, int delay, int width, int height) : m_filePath(filePath) , m_delay(delay) , m_width(width) , m_height(height) {} signals: void imageReady(const QString& image); public slots: void process() { QImageReader reader(m_filePath); reader.setDecideFormatFromContent(true); // 获取 GIF 图像的帧数 int frameCount = reader.imageCount(); for (int i = 0; i < frameCount; i++) { QImage image = reader.read(); image = image.scaled(m_width, m_height, Qt::KeepAspectRatio); // 将图像转换成 ASCII 码 QString asciiImage = imageToAscii(image); // 发送信号,通知主线程处理结果 emit imageReady(asciiImage); // 延时指定的时间 QThread::msleep(m_delay); } } private: QString m_filePath; int m_delay; int m_width; int m_height; // 将图像转换成 ASCII 码 QString imageToAscii(const QImage& image) { QString asciiImage; for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { QColor color = image.pixelColor(x, y); int gray = qGray(color.rgb()); // 根据灰度值选择对应的 ASCII 字符 if (gray >= 230) { asciiImage += " "; } else if (gray >= 200) { asciiImage += "."; } else if (gray >= 180) { asciiImage += "*"; } else if (gray >= 160) { asciiImage += ":"; } else if (gray >= 130) { asciiImage += "o"; } else if (gray >= 100) { asciiImage += "&"; } else if (gray >= 70) { asciiImage += "8"; } else if (gray >= 50) { asciiImage += "#"; } else { asciiImage += "@"; } } asciiImage += "\n"; } return asciiImage; } }; class GifToAscii : public QObject { Q_OBJECT public: GifToAscii(const QString& filePath, int delay, int width, int height) : m_filePath(filePath) , m_delay(delay) , m_width(width) , m_height(height) { m_workerThread = new QThread(); m_worker = new GifToAsciiWorker(m_filePath, m_delay, m_width, m_height); // 将 worker 移动到工作线程中 m_worker->moveToThread(m_workerThread); // 连接信号和槽 connect(m_worker, SIGNAL(imageReady(QString)), this, SLOT(onImageReady(QString))); connect(m_workerThread, SIGNAL(started()), m_worker, SLOT(process())); connect(m_workerThread, SIGNAL(finished()), m_worker, SLOT(deleteLater())); connect(m_workerThread, SIGNAL(finished()), m_workerThread, SLOT(deleteLater())); // 启动工作线程 m_workerThread->start(); } ~GifToAscii() { // 停止工作线程 m_workerThread->quit(); m_workerThread->wait(); } signals: void finished(); public slots: void onImageReady(const QString& image) { // 处理图像 qDebug() << image; } private: QString m_filePath; int m_delay; int m_width; int m_height; QThread* m_workerThread; GifToAsciiWorker* m_worker; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建 GifToAscii 对象 GifToAscii* gifToAscii = new GifToAscii("test.gif", 100, 80, 60); // 等待处理完成 QObject::connect(gifToAscii, SIGNAL(finished()), &a, SLOT(quit())); return a.exec(); } #include "main.moc" ``` 在上面的代码中,我们创建了两个类:GifToAscii 和 GifToAsciiWorker。GifToAscii 类负责创建 GifToAsciiWorker 对象,并将其移动到工作线程中。GifToAsciiWorker 类则负责实际的图像处理工作。 在 GifToAsciiWorker 类的 process() 槽函数中,我们使用 QImageReader 读取 GIF 图像,并使用 imageToAscii() 函数将图像转换成 ASCII 码。然后,我们通过发射 imageReady() 信号将处理结果发送给主线程。 在 GifToAscii 类的 onImageReady() 槽函数中,我们将处理结果打印到控制台。最后,我们在 main() 函数中创建 GifToAscii 对象,并等待处理完成。

相关推荐

最新推荐

recommend-type

Qt中的字符编码转换:UTF8、Unicode、GBK、ASCII、16进制字符、16进制数值

Qt字符转换,串口接收数据转为中文显示可用。Qt中的字符编码转换:UTF8、Unicode、GBK、ASCII、16进制字符、16进制数值
recommend-type

Qt 旋转文字代码实现

Qt显示旋转文字的方式 显示基本方法 利用QPainter基本函数+rotate/translate绘制旋转图形的方法
recommend-type

Qt基础开发之Qt多线程类QThread与Qt定时器类QTimer的详细方法与实例

Qt多线程 我们之前的程序都是单线程运行,接下来我们开始引入多线程。...Qt实现多线程有两种方式 ​1、Qt第一种创建线程方式 首先要继承QThread 重写虚函数QThread::run [virtual protected] void QThread::run()
recommend-type

Qt实现保存、浏览、预览、打印功能的示例代码

下面小编就为大家分享一篇Qt实现保存、浏览、预览、打印功能的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Qt 使用Poppler实现pdf阅读器的示例代码

下面小编就为大家分享一篇Qt 使用Poppler实现pdf阅读器的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。