From 88a4740d813a9c194911a6fb60a59be587c36bf5 Mon Sep 17 00:00:00 2001 From: acetone Date: Thu, 10 Feb 2022 12:06:01 -0500 Subject: [PATCH] Captcha class fixes --- captcha.cpp | 51 ++++++++++++++++++++++++++++++++------------------- captcha.h | 14 +++++++++++++- main.cpp | 4 ++-- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/captcha.cpp b/captcha.cpp index d3657fa..7c66d85 100644 --- a/captcha.cpp +++ b/captcha.cpp @@ -15,8 +15,20 @@ * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* * Copyright (c) 2022 Acetone -* IRCaBot project +* Updated for IRCaBot project +* Usage example: +* Captcha cp; +* cp.randomize(); +* cp.generateText(5); +* QFile f("f.png"); +* if (f.open(QIODevice::WriteOnly)) { +* f.write(cp.captchaPngByteArray()); // ready to use PNG file +* f.close(); +* } +* qInfo() << cp.captchaText(); // answer */ #include "captcha.h" @@ -247,7 +259,8 @@ void Captcha::updateCaptcha() path.setElementPositionAt(i, x, y); } - m_captchaImage = QImage(fm.width(m_captchaText) + m_vmod2 * 2 + m_padding * 2, fm.height() + m_hmod2 * 2 + m_padding * 2, QImage::Format_RGB32); + m_captchaImage = QImage(static_cast(fm.horizontalAdvance(m_captchaText) + m_vmod2 * 2 + m_padding * 2), + static_cast(fm.height() + m_hmod2 * 2 + m_padding * 2), QImage::Format_RGB32); } @@ -264,10 +277,10 @@ void Captcha::updateCaptcha() painter.setPen(QPen(Qt::black, m_lineWidth)); for (int i = 0; i < m_lineCount; i++) { - int x1 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); - int y1 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); - int x2 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); - int y2 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); + int x1 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); + int y1 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); + int x2 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); + int y2 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); painter.drawLine(x1, y1, x2, y2); } painter.setPen(Qt::NoPen); @@ -277,10 +290,10 @@ void Captcha::updateCaptcha() { for (int i = 0; i < m_ellipseCount; i++) { - int x1 = m_ellipseMaxRadius / 2.0 + (static_cast(qrand()) / RAND_MAX) * (m_captchaImage.width() - m_ellipseMaxRadius); - int y1 = m_ellipseMaxRadius / 2.0 + (static_cast(qrand()) / RAND_MAX) * (m_captchaImage.height() - m_ellipseMaxRadius); - int rad1 = m_ellipseMinRadius + (static_cast(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius); - int rad2 = m_ellipseMinRadius + (static_cast(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius); + int x1 = static_cast(m_ellipseMaxRadius / 2.0 + (static_cast(qrand()) / RAND_MAX) * (m_captchaImage.width() - m_ellipseMaxRadius)); + int y1 = static_cast(m_ellipseMaxRadius / 2.0 + (static_cast(qrand()) / RAND_MAX) * (m_captchaImage.height() - m_ellipseMaxRadius)); + int rad1 = static_cast(m_ellipseMinRadius + (static_cast(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius)); + int rad2 = static_cast(m_ellipseMinRadius + (static_cast(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius)); painter.setBrush(backColor()); painter.setCompositionMode(QPainter::CompositionMode_Difference); painter.drawEllipse(QPoint(x1, y1), rad1, rad2); @@ -291,10 +304,10 @@ void Captcha::updateCaptcha() { for (int i = 0; i < m_noiseCount; i++) { - int x1 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); - int y1 = (static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); + int x1 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.width(); + int y1 = static_cast(static_cast(qrand()) / RAND_MAX) * m_captchaImage.height(); - QColor col = QColor((static_cast(qrand()) / RAND_MAX) * 255, (static_cast(qrand()) / RAND_MAX) * 255, (static_cast(qrand()) / RAND_MAX) * 255); + QColor col = QColor(static_cast(static_cast(qrand()) / RAND_MAX) * 255, static_cast(static_cast(qrand()) / RAND_MAX) * 255, static_cast(static_cast(qrand()) / RAND_MAX) * 255); painter.setPen(QPen(col, m_noisePointSize)); painter.setCompositionMode(QPainter::CompositionMode_SourceOver); @@ -441,15 +454,15 @@ void Captcha::generateText(int noOfChars, bool includeNumbers, bool includeSymbo QVector chars; for (int i = 0; i < noOfChars * 2; i++) { - chars.push_back(65 + (static_cast(qrand()) / RAND_MAX) * (90 - 65)); - if (!allCapital) chars.push_back(97 + (static_cast(qrand()) / RAND_MAX) * (122 - 97)); - if (includeNumbers) chars.push_back(48 + (static_cast(qrand()) / RAND_MAX) * (57 - 48)); - if (includeSymbols) chars.push_back(33 + (static_cast(qrand()) / RAND_MAX) * (47 - 33)); + chars.push_back(65 + static_cast(static_cast(qrand()) / RAND_MAX) * (90 - 65)); + if (!allCapital) chars.push_back(97 + static_cast(static_cast(qrand()) / RAND_MAX) * (122 - 97)); + if (includeNumbers) chars.push_back(48 + static_cast(static_cast(qrand()) / RAND_MAX) * (57 - 48)); + if (includeSymbols) chars.push_back(33 + static_cast(static_cast(qrand()) / RAND_MAX) * (47 - 33)); } for (int i = 0; i < noOfChars; i++) { - text += chars[(qrand() / (qreal) RAND_MAX) * (chars.size() - 1.0)]; + text += static_cast(chars[static_cast((qrand() / static_cast(RAND_MAX)) * (chars.size() - 1.0))]); } m_captchaText = text; @@ -461,7 +474,7 @@ void Captcha::generateText(int noOfChars, bool includeNumbers, bool includeSymbo qWarning() << "In text generation : Dictionary size is too small"; return; } - m_captchaText = m_dictionary[(qrand() / (qreal) RAND_MAX) * (m_dictionary.size() - 1.0)]; + m_captchaText = m_dictionary[static_cast((qrand() / static_cast(RAND_MAX)) * (m_dictionary.size() - 1.0))]; } else { diff --git a/captcha.h b/captcha.h index ee7a119..eb0a21a 100644 --- a/captcha.h +++ b/captcha.h @@ -15,8 +15,20 @@ * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. * +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* * Copyright (c) 2022 Acetone -* IRCaBot project +* Updated for IRCaBot project +* Usage example: +* Captcha cp; +* cp.randomize(); +* cp.generateText(5); +* QFile f("f.png"); +* if (f.open(QIODevice::WriteOnly)) { +* f.write(cp.captchaPngByteArray()); // ready to use PNG file +* f.close(); +* } +* qInfo() << cp.captchaText(); // answer */ #ifndef CAPTCHA_H diff --git a/main.cpp b/main.cpp index a8b205b..0d00073 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,7 @@ #include "version.h" #include -#include +#include #include int main(int argc, char *argv[]) @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) return 1; } - QCoreApplication a(argc, argv); + QApplication a(argc, argv); QString configFile; for (int i = 1; i < argc; i++) {