QT case combat 1 - write an OCR tool software from scratch (8) Pdf reading display / screenshot / text recognition

1. PDF reading display function

        QT provides pdf, PdfWidgets modules, you need to install the modules, and then add the following code in the project's CMakeLists.txt file for use in the project.

        The official provides a complete pdf display example, example 1: a widget-based PDF viewer that allows scrolling pages. Example 2: A Qt Quick PDF viewer that allows scrolling pages.

https://doc.qt.io/qt-6/qtpdf-examples.html icon-default.png?t=M85Bhttps://doc.qt.io/qt-6/qtpdf-examples.html         CMakeLists.txt file.

find_package(Qt6 REQUIRED COMPONENTS Pdf)
find_package(Qt6 REQUIRED COMPONENTS PdfWidgets)

target_link_libraries(QtFFmpegApp2 PRIVATE Qt6::Pdf)
target_link_libraries(QtFFmpegApp2 PRIVATE Qt6::PdfWidgets)

2. Preliminary design of functions 

1. Function brief

        The pdf is displayed on the left, and the display area of ​​the result of the ocr recognition is on the right.

2, pdfview control

        The control needs to be promoted to QPdfView, which cannot be seen in the left menu of the design.

    m_document = new QPdfDocument(this);

    //目录,我这里不需要目录,所以没有展示
    //QPdfBookmarkModel *bookmarkModel = new QPdfBookmarkModel(this);
    //bookmarkModel->setDocument(m_document);
    //ui->bookmarkView->setModel(bookmarkModel);

    ui->pdfView->setZoomMode(QPdfView::FitToWidth);
    //ui->pdfView->setZoomFactor(0.8);
    ui->pdfView->setDocument(m_document);

3. Read pdf

        Click the button to select the pdf button, select the pdf and call the open method to open it.

void PdfViewWindow::on_pushButton_clicked()
{
    QUrl toOpen = QFileDialog::getOpenFileUrl(this, tr("Choose a PDF"), QUrl(), "Portable Documents (*.pdf)");
       if (toOpen.isValid())
           open(toOpen);
}

4. Display pdf

        Here, as long as the path of the local pdf file is passed to QPdfDocument, the pdf file can be opened.

void PdfViewWindow::open(const QUrl &docLocation)
{
    if (docLocation.isLocalFile()) {
        m_document->load(docLocation.toLocalFile());
        const auto documentTitle = m_document->metaData(QPdfDocument::Title).toString();
        setWindowTitle(!documentTitle.isEmpty() ? documentTitle : QStringLiteral("PDF Viewer"));
        ui->label->setText("共" + QString::number(ui->pdfView->pageNavigation()->pageCount()) + "页");
        ui->lineEdit->setText(QString::number(1));
    } else {
        QMessageBox::critical(this, tr("Failed to open"), tr("%1 is not a valid local file").arg(docLocation.toString()));
    }
}

5. Screenshot/Text Recognition

        QPdfDocument provides us with a render method that allows us to get screenshots. After getting the picture, start a new thread and perform ocr identification. The thread here is the same as the ocr of the previous picture, and the processing method is exactly the same.

/**
 * @brief PdfViewWindow::on_pushButton_3_clicked
 * 文字提取
 */
void PdfViewWindow::on_pushButton_3_clicked()
{
    QString filePath = QString("%1\\screen.jpg").arg(qApp->applicationDirPath().replace("/", "\\"));

    //渲染到图片
    //size = m_document->pageSize(1);//QSize(600, 800)
    int page = ui->pdfView->pageNavigation()->currentPage();
    QSize origin = m_document->pageSize(page).toSize();
    QSize *newsize = new QSize(origin.width()*2, origin.height()*2);
    QImage image = m_document->render(page, *newsize);
    image.save(filePath, "jpg");

    //清空右侧文本框
    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,&PdfViewWindow::getRecognitionText);
    connect(m_thread, &MyThreadForTextRecognition::recognitionFinish,this,&PdfViewWindow::recognitionFinish);
    m_thread->start();
}

Guess you like

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