项目中用到的利用opencv库识别数字,例子比较简单,本身没什么价值,简单记录一下。上代码:
#pragma once
#include <QObject>
#include <QFile>
#include <QDebug>
#include <QTextCodec>
#include "IEvent.h"
#include "clickevent.h"
#include "judgeevent.h"
#include "executor.h"
#include "cvIdentify.h"
#include <QDir>
#include <QThread>
#include <QStandardItem>
#include <QMutex>
class executorThread : public QObject
{
Q_OBJECT
public:
executorThread(QObject *parent=0);
~executorThread();
public slots:
void test(const QStringList& fileName, const QVector<QStandardItem*> m_box);
void pauseThread(bool);
void stopThread();
private:
bool importCmdFromDat(QString casePath, QList<QPair<int, QString> >& steps);
signals:
void sig_showExample(QList<QPair<int, QString> >);
void sig_showResult(QList<QPair<int, QString> > step, QPair<int, QString> cmd, bool rel, QString des);
void sig_identifyResult(int passCount, int executCount, int step,bool resOk);
private:
QMutex m_lock;
bool isPause;
bool isStop;
};
#include "executorThread.h"
executorThread::executorThread(QObject *parent)
: QObject(parent)
{
isStop = false;
}
executorThread::~executorThread()
{
}
void executorThread::pauseThread(bool status)
{
//isStop = status;
if (status)
{
m_lock.lock();//暂停
}
else
{
m_lock.unlock();//恢复
}
}
void executorThread::stopThread()
{
isStop = true;
}
void executorThread::test(const QStringList& fileName, const QVector<QStandardItem*> m_box)
{
//qDebug() << QString("my object thread id:") << QThread::currentThreadId();
//注册执行器的事件对象
if (!m_box.isEmpty())
{
Executor* exep = new Executor();
exep->attach(new ClickEvent());
exep->attach(new JudgeEvent());
int executCount = 0;
int passCount = 0;
for (int i = 0; i < fileName.size(); ++i)
{
QList<QPair<int, QString> > stepList;
if (importCmdFromDat(fileName.at(i), stepList))
{
emit sig_showExample(stepList);
if (m_box.at(i)->checkState())
{
bool eveOk; // 事件是否 正常执行
bool resOk = true; // 结果是否 正确
QString exeDis; // 结果的具体描述
foreach(auto step, stepList)
{
QMutexLocker locker(&m_lock);
if (isStop)
{
return;
}
resOk = true;
exeDis.clear();
eveOk = exep->exeCmd(step.second, resOk, exeDis);
emit sig_showResult(stepList,step, eveOk,exeDis);
// 对执行结果 处理, 打印, 保存
// todo
}
executCount++;
if (resOk)
{
passCount++;
}
emit sig_identifyResult(passCount,executCount,i,resOk);
//isStop = true;
}
}
}
}
}
bool executorThread::importCmdFromDat(QString casePath, QList<QPair<int, QString> >& steps)
{
// 按 行读取用例文件
// 过滤掉 注释行和 空行
// 将 测试步骤 添加到 steps中
// 成功 读 入 返回 true
// todo
//return true;
// 按 行读取用例文件
QTextCodec* codec = QTextCodec::codecForName("GBK");
QFile file(casePath);
if (!file.exists())
{
return false;
}
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QPair<int, QString> p1;
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
//QString str = codec->toUnicode(line);
str = str.trimmed(); //过滤掉空白字符串
if (str.startsWith("//") || str.isEmpty()) //过滤掉注释行以及空白行
{
continue;
}
p1.first++;
p1.second = str;
steps.append(p1); // 将其添加到steps中
}
file.close();
}
return true;
}