当前位置:   article > 正文

c++11 string转ing_Qt Gif转字符动画

c++ string转qt图片

2b748aa3bf15faeebd3ce236387cb761.gif

简述

今天无意间看到一位网友用Java实现了一个 GIF转字符动画的效果,看起来挺有意思的。然后看了下原理,用Qt也实现了一把。目前缺陷是计算过程中特别耗CPU,后面的网友们,希望你们能优化掉这个问题。

参考地址:

【轻松一刻】Java制作字符动画​blog.csdn.net

效果图

83dcf6f968632136f60707f05a610a42.gif

代码

  1. const int limit_max_pixmap_cache = 5;
  2. char toChar(int g)
  3. {
  4. if (g <= 30) {
  5. return '#';
  6. }
  7. else if (g > 30 && g <= 60) {
  8. return '&';
  9. }
  10. else if (g > 60 && g <= 120) {
  11. return '$';
  12. }
  13. else if (g > 120 && g <= 150) {
  14. return '*';
  15. }
  16. else if (g > 150 && g <= 180) {
  17. return 'o';
  18. }
  19. else if (g > 180 && g <= 210) {
  20. return '!';
  21. }
  22. else {
  23. return ' ';
  24. }
  25. }
  26. int rgbtoGray(int r, int g, int b)
  27. {
  28. return (int)((((quint32)((r << 5) + (r << 2) + (r << 1))) + (quint32)((g << 6) + (g << 3) + (g << 1) + g)
  29. + (quint32)((b << 4) - b)) >> 7);
  30. }
  31. QPixmap setImage(const QImage &image)
  32. {
  33. const int ih = image.height();
  34. const int iw = image.width();
  35. QPixmap txtImage(iw, ih);
  36. txtImage.fill(Qt::transparent);
  37. QPainter painter(&txtImage);
  38. painter.setBrush(Qt::NoBrush);
  39. painter.setPen(QColor::fromHsl(rand() % 360, rand() % 256, rand() % 200));
  40. QFont font = painter.font();
  41. font.setPixelSize(7);
  42. font.setFamily("Microsoft YaHei");
  43. painter.setFont(font);
  44. for (int i = 0; i < iw; i+= 7)
  45. {
  46. for (int j = 0; j < ih; j+= 7)
  47. {
  48. const QRgb&& pixel = image.pixel(i, j);
  49. int r = qRed(pixel);
  50. int g = qGreen(pixel);
  51. int b = qBlue(pixel);
  52. int gray = rgbtoGray(r, g, b);
  53. char c = toChar((int)gray);
  54. painter.drawText(i, j, QChar(c));
  55. }
  56. }
  57. return txtImage;
  58. }
  59. MainWindow::MainWindow(QWidget *parent)
  60. : QWidget(parent)
  61. {
  62. ui.setupUi(this);
  63. m_movie = getMovie(":/MainWindow/Resources/1.gif");
  64. m_movieFrameCount = m_movie->frameCount();
  65. m_curplayFrame = 0;
  66. m_movie->jumpToFrame(0);
  67. for (int i = 0; i < limit_max_pixmap_cache; i++){
  68. m_cachePixmap.insert(i, setImage(m_movie->currentImage()));
  69. m_srcCachePixmap.insert(i, m_movie->currentPixmap());
  70. m_movie->jumpToNextFrame();
  71. }
  72. updateTextPixmap();
  73. }
  74. MainWindow::~MainWindow()
  75. {
  76. QThreadPool::globalInstance()->clear();
  77. QThreadPool::globalInstance()->waitForDone(3000);
  78. }
  79. void MainWindow::updateTextPixmap()
  80. {
  81. int curNumber = m_movie->currentFrameNumber();
  82. if (m_cachePixmap.contains(m_curplayFrame)){
  83. ui.imageLabel->setPixmap(m_cachePixmap.value(m_curplayFrame));
  84. ui.giflabel->setPixmap(m_srcCachePixmap.value(m_curplayFrame));
  85. m_cachePixmap.take(m_curplayFrame);
  86. m_srcCachePixmap.take(m_curplayFrame);
  87. ThradFunctionTask* task = new ThradFunctionTask;
  88. int curplayFrame = m_curplayFrame;
  89. QImage image = m_movie->currentImage();
  90. task->FNBind([this, curNumber, curplayFrame, image](){
  91. m_cachePixmap.insert(curNumber, setImage(image));
  92. m_srcCachePixmap.insert(curNumber, QPixmap::fromImage(image));
  93. });
  94. QThreadPoolManager::getInstance()->AddTask(task);
  95. }
  96. else{
  97. qDebug() << "totalframe = " << m_movieFrameCount << " not find = " << m_curplayFrame;
  98. }
  99. m_curplayFrame++;
  100. if (m_curplayFrame >= m_movieFrameCount)
  101. m_curplayFrame = 0;
  102. m_movie->jumpToNextFrame();
  103. QTimer::singleShot(m_movie->speed(), this, &MainWindow::updateTextPixmap);
  104. }
  105. QMovie* MainWindow::getMovie(const QString& path)
  106. {
  107. if (m_movie == nullptr)
  108. m_movie = new QMovie(this);
  109. if (m_movie->state() == QMovie::NotRunning){
  110. m_movie->setFileName(path);
  111. m_movie->jumpToFrame(0);
  112. m_movie->stop();
  113. }
  114. return m_movie;
  115. }

结尾

更新精彩Qt作品内容,欢迎关注雨田哥的知乎
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/94297
推荐阅读
相关标签
  

闽ICP备14008679号