Captcha class fixes

master
const an teen 2022-02-10 12:06:01 -05:00
parent a387305498
commit 88a4740d81
3 changed files with 47 additions and 22 deletions

View File

@ -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<int>(fm.horizontalAdvance(m_captchaText) + m_vmod2 * 2 + m_padding * 2),
static_cast<int>(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<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y1 = (static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.height();
int x2 = (static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y2 = (static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.height();
int x1 = static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y1 = static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.height();
int x2 = static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y2 = static_cast<int>(static_cast<qreal>(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<qreal>(qrand()) / RAND_MAX) * (m_captchaImage.width() - m_ellipseMaxRadius);
int y1 = m_ellipseMaxRadius / 2.0 + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_captchaImage.height() - m_ellipseMaxRadius);
int rad1 = m_ellipseMinRadius + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius);
int rad2 = m_ellipseMinRadius + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius);
int x1 = static_cast<int>(m_ellipseMaxRadius / 2.0 + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_captchaImage.width() - m_ellipseMaxRadius));
int y1 = static_cast<int>(m_ellipseMaxRadius / 2.0 + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_captchaImage.height() - m_ellipseMaxRadius));
int rad1 = static_cast<int>(m_ellipseMinRadius + (static_cast<qreal>(qrand()) / RAND_MAX) * (m_ellipseMaxRadius - m_ellipseMinRadius));
int rad2 = static_cast<int>(m_ellipseMinRadius + (static_cast<qreal>(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<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y1 = (static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.height();
int x1 = static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.width();
int y1 = static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * m_captchaImage.height();
QColor col = QColor((static_cast<qreal>(qrand()) / RAND_MAX) * 255, (static_cast<qreal>(qrand()) / RAND_MAX) * 255, (static_cast<qreal>(qrand()) / RAND_MAX) * 255);
QColor col = QColor(static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * 255, static_cast<int>(static_cast<qreal>(qrand()) / RAND_MAX) * 255, static_cast<int>(static_cast<qreal>(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<unsigned char> chars;
for (int i = 0; i < noOfChars * 2; i++)
{
chars.push_back(65 + (static_cast<qreal>(qrand()) / RAND_MAX) * (90 - 65));
if (!allCapital) chars.push_back(97 + (static_cast<qreal>(qrand()) / RAND_MAX) * (122 - 97));
if (includeNumbers) chars.push_back(48 + (static_cast<qreal>(qrand()) / RAND_MAX) * (57 - 48));
if (includeSymbols) chars.push_back(33 + (static_cast<qreal>(qrand()) / RAND_MAX) * (47 - 33));
chars.push_back(65 + static_cast<unsigned char>(static_cast<qreal>(qrand()) / RAND_MAX) * (90 - 65));
if (!allCapital) chars.push_back(97 + static_cast<unsigned char>(static_cast<qreal>(qrand()) / RAND_MAX) * (122 - 97));
if (includeNumbers) chars.push_back(48 + static_cast<unsigned char>(static_cast<qreal>(qrand()) / RAND_MAX) * (57 - 48));
if (includeSymbols) chars.push_back(33 + static_cast<unsigned char>(static_cast<qreal>(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<char>(chars[static_cast<int>((qrand() / static_cast<qreal>(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<int>((qrand() / static_cast<qreal>(RAND_MAX)) * (m_dictionary.size() - 1.0))];
}
else
{

View File

@ -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

View File

@ -6,7 +6,7 @@
#include "version.h"
#include <QThread>
#include <QCoreApplication>
#include <QApplication>
#include <QDebug>
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++) {