opencv实现数字匹配

项目中用到的利用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值