QT Case Practice 1 - Write an OCR Tool Software from Scratch (7) Screenshot / Image Display / Text Recognition

1. Image text recognition function

        In the previous article, the ocr recognition engine was introduced, and the call engine was also implemented in the thread for recognition. With all the basic functions, you can organize the functions and UI together.

        Regarding the function of image and text recognition, the UI is divided into two parts. The left side realizes the functions of screenshot, opening image and image display.

         On the right side is the function of selecting OCR engine, selecting language, text extraction and simple processing after extraction.

2. Function description

1. Screenshots

        The screenshot function is the same as the implementation linked below.

Audio and Video Learning - Implementing the Screenshot Function in the Qt 6.3.1 version In the Qt project, the module that implements the screenshot function is implemented in detail (can be universal) (2) The showEvent method mainly modifies the pixel ratio of the acquisition device, so as to avoid problems with screenshots under high-resolution devices. The basics are similar to the reference blog, mainly modifying the ScreenWidget and showEvent methods. QDesktopWidget is deprecated, use QGuiApplication instead. The p in the code is the pointer of the bottommost window, first minimize the window from the bottommost layer, and then display the screenshot layer. (1) The ScreenWidget side modified the way to obtain the screen size. https://skydance.blog.csdn.net/article/details/126789405

2. Open the picture

//选择图片
filePath = QFileDialog::getOpenFileName(this, tr("选择图片"), ".", tr("Image Files(*.jpg *.png)"));

//加载图片
QImage* image = new QImage;
image->load(filePath);

//对图片进行缩放展示
QPixmap picScale = QPixmap::fromImage(*image).scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

//在UI上展示图片
ui->label->setPixmap(picScale);

3. Text extraction

        The main purpose here is to pass the image path to the ocr recognition thread , and then the getRecognitionText function will be called during the ocr thread recognition process, and the recognitionFinish function will be called after the recognition is completed.

void TextRecognition::on_pushButton_2_clicked()
{
    if(filePath.isEmpty())
    {
        QString dlgTitle = "提示";
        QString strInfo = QString::fromStdString("没有选择图片");
        QMessageBox::information(this, dlgTitle, strInfo, QMessageBox::Ok, QMessageBox::NoButton);
        return;
    }

    ui->textEdit->setText("");

    //实例化loading窗口
    loading = new LoadingDialog(this);
    loading->setVisible(true);

    //启动线程
    m_thread  =  new MyThreadForTextRecognition;
    m_thread->init(filePath.toStdString(), QString("%1\\screen_action.jpg").arg(qApp->applicationDirPath().replace("/", "\\")).toStdString(),
                   ui->comboBox->currentIndex(), ui->checkBox_3->isChecked(), ui->comboBox_2->currentIndex());
    connect(m_thread, &MyThreadForTextRecognition::getRecognitionText,this,&TextRecognition::getRecognitionText);
    connect(m_thread, &MyThreadForTextRecognition::recognitionFinish,this,&TextRecognition::recognitionFinish);
    m_thread->start();
}

4. Slot function

        After the getRecognitionText function receives the recognition result, it will process the text according to whether the checkbox option deletes spaces and newlines.

        After the recognitionFinish function is called, the loading will be closed.

void TextRecognition::getRecognitionText(std::string outText)
{
    if(outText.empty())
    {
        QString dlgTitle = "提示";
        QString strInfo = QString::fromStdString("解析失败");
        QMessageBox::information(this, dlgTitle, strInfo, QMessageBox::Ok, QMessageBox::NoButton);
        return ;
    }

    std::string str3 = outText;
     qDebug() << "识别结果:" << str3.c_str();
    //循环处理字符串,主要是为了删除一些,不过需要仔细考虑
    int index = 0;
    if (!str3.empty())
    {
        //如果是中文
        //if(get_Choise_Language().rfind("chi", 0) == 0)
        if(ui->checkBox_2->isChecked())
        {
            //删除换行符
            str3.erase(std::remove(str3.begin(), str3.end(), '\n'), str3.end());
        }

        if(ui->checkBox->isChecked())
        {
            //循环删除所有空格
            while ((index = str3.find(' ', index)) != std::string::npos)
            {
                str3.erase(index, 1);
            }
        }
    }

    QString str(str3.c_str());
    ui->textEdit->append(str + '\n');
    //delete [] outText;
}

/**
 * @brief TextRecognition::recognitionFinish
 * 识别完成
 */
void TextRecognition::recognitionFinish()
{
    loading->setVisible(false);
}

5. Rendering

Guess you like

Origin blog.csdn.net/bashendixie5/article/details/127157171