void Ocr()
{
QString currentPath = QDir::currentPath();
currentPath = currentPath + "/image_grab/ocr.bmp";
string strCmd = "tesseract " + currentPath.toStdString() + " stdout";
qDebug() << currentPath;
char buf[10240] = { 0 };
FILE* pf = NULL;
AllocConsole();//新建窗口
ShowWindow(GetConsoleWindow(), SW_HIDE);//隐藏
if ((pf = _popen(strCmd.c_str(), "r")) == NULL)
{
return;
}
string strResult;
while (fgets(buf, sizeof buf, pf))
{
strResult += buf;
}
_pclose(pf);
unsigned int iSize = strResult.size();
if (iSize > 0 && strResult[iSize - 1] == '\n') // linux
{
strResult = strResult.substr(0, iSize - 1);
}
QString result = QString(QString::fromLocal8Bit(strResult.c_str()));
QStringList resList;
resList = result.split("\n");
result = resList.join(" ");
QMessageBox::information(this, "Ocr result", result);
/// 以管理员的身份运行程序并返回结果
/// runas
//string cmd = string("runas /noprofile /user:mymachine\\administrator \"cmd /c ") + "cmd /c chcp 65001 && ping www.baidu.com -n 1 -w 1000" + "\"";
//strRe = getCmdResult(cmd);
//qDebug() << strRe.c_str();
}
Qt 调用CMD 并返回结果(隐藏窗口)
于 2023-02-24 18:54:06 首次发布
这段代码演示了一个OCR(OpticalCharacterRecognition)过程,它使用Tesseract工具读取当前目录下image_grab/ocr.bmp图像文件,并将识别出的文本输出到控制台。识别结果被存储在strResult中,最终显示在一个消息框中。此外,代码还包含了以管理员权限运行程序的注释部分。
摘要由CSDN通过智能技术生成