captcha.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2014 Omkar Kanase
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. *
  18. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  19. *
  20. * Copyright (c) 2022 Acetone
  21. * Updated for IRCaBot project
  22. * Usage example:
  23. * #include <QApplication>
  24. * int main(int argc, char *argv[]) {
  25. * QApplication a(argc, argv);
  26. * Captcha cp;
  27. * cp.randomize();
  28. * cp.generateText(5);
  29. * QFile f("f.png");
  30. * if (f.open(QIODevice::WriteOnly)) {
  31. * f.write(cp.captchaPngByteArray()); // ready to use PNG file
  32. * f.close();
  33. * }
  34. * qInfo() << cp.captchaText(); // answer
  35. * }
  36. */
  37. #ifndef CAPTCHA_H
  38. #define CAPTCHA_H
  39. #include <QObject>
  40. #include <QFont>
  41. #include <QImage>
  42. #include <QTime>
  43. #include <QVector>
  44. #include <QStringList>
  45. #include <QDebug>
  46. #include <QFile>
  47. #include <QTextStream>
  48. #include <QPainter>
  49. #include <QPainterPath>
  50. #include <QtMath>
  51. #include <QBuffer>
  52. class Captcha : public QObject
  53. {
  54. Q_OBJECT
  55. Q_ENUMS(DeformType)
  56. Q_ENUMS(TextGenerationMode)
  57. Q_PROPERTY(QFont font READ font WRITE setFont)
  58. Q_PROPERTY(QImage captchaImage READ captchaImage)
  59. Q_PROPERTY(QString captchaText READ captchaText WRITE setCaptchaText)
  60. Q_PROPERTY(DeformType deformationType READ deformationType WRITE setDeformationType)
  61. Q_PROPERTY(TextGenerationMode textGeneration READ textGeneration WRITE setTextGeneration)
  62. Q_PROPERTY(QStringList dictionary READ dictionary WRITE setDictionary)
  63. Q_PROPERTY(QColor fontColor READ fontColor WRITE setFontColor)
  64. Q_PROPERTY(QColor backColor READ backColor WRITE setBackColor)
  65. Q_PROPERTY(bool drawLines READ drawLines WRITE setDrawLines)
  66. Q_PROPERTY(bool drawEllipses READ drawEllipses WRITE setDrawEllipses)
  67. Q_PROPERTY(bool drawNoise READ drawNoise WRITE setDrawNoise)
  68. Q_PROPERTY(int noiseCount READ noiseCount WRITE setNoiseCount)
  69. Q_PROPERTY(int lineCount READ lineCount WRITE setLineCount)
  70. Q_PROPERTY(int ellipseCount READ ellipseCount WRITE setEllipseCount)
  71. Q_PROPERTY(int lineWidth READ lineWidth WRITE setLineWidth)
  72. Q_PROPERTY(int ellipseMinRadius READ ellipseMinRadius WRITE setEllipseMinRadius)
  73. Q_PROPERTY(int ellipseMaxRadius READ ellipseMaxRadius WRITE setEllipseMaxRadius)
  74. Q_PROPERTY(int noisePointSize READ noisePointSize WRITE setNoisePointSize)
  75. public:
  76. enum DeformType
  77. {
  78. Deform_SinCurve
  79. };
  80. enum TextGenerationMode
  81. {
  82. TextGeneration_Random,
  83. TextGeneration_Dictionary
  84. };
  85. public:
  86. explicit Captcha(QObject *parent = nullptr);
  87. QByteArray captchaPngByteArray() const;
  88. QFont font() const;
  89. QImage captchaImage() const;
  90. DeformType deformationType() const;
  91. QString captchaText() const;
  92. TextGenerationMode textGeneration() const;
  93. const QStringList &dictionary() const;
  94. QColor fontColor() const;
  95. QColor backColor() const;
  96. bool drawLines() const;
  97. bool drawEllipses() const;
  98. bool drawNoise() const;
  99. int noiseCount() const;
  100. int lineCount() const;
  101. int ellipseCount() const;
  102. int lineWidth() const;
  103. int ellipseMinRadius() const;
  104. int ellipseMaxRadius() const;
  105. int noisePointSize() const;
  106. signals:
  107. void captchaGenerated(const QImage& img, QString text);
  108. public slots:
  109. void setFont(const QFont& arg);
  110. void setDeformationType(DeformType arg);
  111. void updateCaptcha();
  112. void randomize();
  113. void generateText(int noOfChars = 5, bool includeNumbers = false, bool includeSymbols = false, bool allCapital = true);
  114. void setCaptchaText(QString arg);
  115. void setTextGeneration(TextGenerationMode arg);
  116. void setDictionary(const QStringList& arg);
  117. void loadDictionary(QString FileName);
  118. void setFontColor(QColor arg);
  119. void setBackColor(QColor arg);
  120. void setSinDeform(qreal hAmplitude, qreal hFrequency, qreal vAmplitude, qreal vFrequency);
  121. QPair<QString, QImage> generateCaptcha();
  122. void setDrawLines(bool arg);
  123. void setDrawEllipses(bool arg);
  124. void setDrawNoise(bool arg);
  125. void setNoiseCount(int arg);
  126. void setLineCount(int arg);
  127. void setEllipseCount(int arg);
  128. void setLineWidth(int arg);
  129. void setEllipseMinRadius(int arg);
  130. void setEllipseMaxRadius(int arg);
  131. void setNoisePointSize(int arg);
  132. void setDifficulty(int val);
  133. private:
  134. qreal m_hmod1;
  135. qreal m_hmod2;
  136. qreal m_vmod1;
  137. qreal m_vmod2;
  138. QFont m_font;
  139. QImage m_captchaImage;
  140. DeformType m_deformationType;
  141. QString m_captchaText;
  142. TextGenerationMode m_textGeneration;
  143. QStringList m_dictionary;
  144. QColor m_fontColor;
  145. QColor m_backColor;
  146. qreal m_padding;
  147. bool m_drawLines;
  148. bool m_drawEllipses;
  149. bool m_drawNoise;
  150. int m_noiseCount;
  151. int m_lineCount;
  152. int m_ellipseCount;
  153. int m_lineWidth;
  154. int m_ellipseMinRadius;
  155. int m_ellipseMaxRadius;
  156. int m_noisePointSize;
  157. };
  158. #endif // CAPTCHA_H