imagezoomandgray.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "imagezoomandgray.h"
  2. ImageZoomAndGray::ImageZoomAndGray(QObject* parent) : QObject(parent)
  3. {
  4. m_zoomWidth = 40;
  5. m_zoomHeight = 40;
  6. }
  7. int ImageZoomAndGray::zoomWidth() const
  8. {
  9. return m_zoomWidth;
  10. }
  11. void ImageZoomAndGray::setZoomWidth(int zoomWidth)
  12. {
  13. m_zoomWidth = zoomWidth;
  14. }
  15. int ImageZoomAndGray::zoomHeight() const
  16. {
  17. return m_zoomHeight;
  18. }
  19. void ImageZoomAndGray::setZoomHeight(int zoomHeight)
  20. {
  21. m_zoomHeight = zoomHeight;
  22. }
  23. QString ImageZoomAndGray::exportFolder() const
  24. {
  25. return m_exportFolder;
  26. }
  27. void ImageZoomAndGray::setExportFolder(const QString& exportFolder)
  28. {
  29. m_exportFolder = exportFolder;
  30. }
  31. void ImageZoomAndGray::startProc()
  32. {
  33. int i = 1;
  34. for (QString path : imageFileNames)
  35. {
  36. QPixmap pix(path);
  37. pix = pix.scaled(m_zoomWidth, m_zoomHeight);
  38. QImage img = pix.toImage();
  39. img = toGray(img);
  40. img.save(m_exportFolder + QString("%1.jpg").arg(i));
  41. i++;
  42. }
  43. QApplication::processEvents();
  44. }
  45. void ImageZoomAndGray::openFolder(QString path)
  46. {
  47. QDir dir(path);
  48. QFileInfoList fileList = dir.entryInfoList();
  49. for (QFileInfo info : fileList)
  50. {
  51. if (info.suffix() == "jpg")
  52. {
  53. imageFileNames << info.absoluteFilePath();
  54. }
  55. }
  56. }
  57. QImage ImageZoomAndGray::toGray(QImage image)
  58. {
  59. int height = image.height();
  60. int width = image.width();
  61. QImage ret(width, height, QImage::Format_Indexed8);
  62. ret.setColorCount(256);
  63. for (int i = 0; i < 256; i++)
  64. {
  65. ret.setColor(i, qRgb(i, i, i));
  66. }
  67. switch (image.format())
  68. {
  69. case QImage::Format_Indexed8:
  70. for (int i = 0; i < height; i ++)
  71. {
  72. const uchar* pSrc = (uchar*)image.constScanLine(i);
  73. uchar* pDest = (uchar*)ret.scanLine(i);
  74. memcpy(pDest, pSrc, width);
  75. }
  76. break;
  77. case QImage::Format_RGB32:
  78. case QImage::Format_ARGB32:
  79. case QImage::Format_ARGB32_Premultiplied:
  80. for (int i = 0; i < height; i ++)
  81. {
  82. const QRgb* pSrc = (QRgb*)image.constScanLine(i);
  83. uchar* pDest = (uchar*)ret.scanLine(i);
  84. for (int j = 0; j < width; j ++)
  85. {
  86. pDest[j] = qGray(pSrc[j]);
  87. }
  88. }
  89. break;
  90. }
  91. return ret;
  92. }