Skip to content

Commit

Permalink
文档处理 预览一页OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Jan 17, 2024
1 parent d60a601 commit 356ebff
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
27 changes: 26 additions & 1 deletion UmiOCR-data/py_src/mission/doc_preview_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
from .simple_mission import SimpleMission
from ..image_controller.image_provider import PixmapProvider
from ..utils.call_func import CallFunc
from .mission_doc import MissionDOC


# 文档预览连接器
class DocPreviewConnector(QObject):
previewImg = Signal(str)
previewImg = Signal(str) # imgID
previewOcr = Signal("QVariant") # [path, page, res]
# 注:信号中含多个变量可能导致崩溃?

def __init__(self, *args):
super().__init__(*args)
self._previewMission = SimpleMission(self._previewTask) # 简单任务对象
self._previewDoc = None # 当前预览的对象
self._previewPath = ""

# 预览PDF画面
@Slot(str, int, str)
def preview(self, path, page, password):
page -= 1
Expand Down Expand Up @@ -61,6 +65,27 @@ def _previewTask(self, msn):
imgID = PixmapProvider.addPixmap(qpixmap)
self.previewImg.emit(imgID)

# 预览一页OCR内容
@Slot(str, int, str, "QVariant")
def ocr(self, path, page, password, argd):
argd = argd.toVariant() # qml对象转python字典

def _onGet(msnInfo, page_, res):
page_ += 1
self.previewOcr.emit([path, page_, res])

def _onEnd(msnInfo, msg):
if not msg.startswith("[Success]"):
res = {"code": 103, "data": msg}
self.previewOcr.emit([path, -1, res])

msnInfo = {
"argd": argd,
"onGet": _onGet,
"onEnd": _onEnd,
}
MissionDOC.addMission(msnInfo, path, (page, page), password=password)

# 清空缓存
@Slot()
def clear(self):
Expand Down
57 changes: 53 additions & 4 deletions UmiOCR-data/qt_res/qml/TabPages/BatchDOC/PreviewDoc.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ ModalLayer {
property var configsComp: undefined // 设置组件
property string ignoreAreaKey: "" // 设置组件中忽略区域的key

property bool running: false
property string previewPath: ""
property string password: ""
property bool isEncrypted: false // 已加密
Expand All @@ -24,6 +23,8 @@ ModalLayer {
property int pageCount: -1
property int rangeStart: -1
property int rangeEnd: -1
property bool previewOCR: false // 是否预览OCR
property bool ocrRunning: false // 是否预览OCR正在执行

// 展示文档
// info: path, page_count, range_start, range_end, is_encrypted, password, is_authenticate
Expand Down Expand Up @@ -94,6 +95,7 @@ ModalLayer {
imgViewer.clear()
prevConn.clear() // 清除文档缓存
qmlapp.popup.simple(qsTr("文档信息已更新"), previewPath)
previewPath = ""
}

// 翻页。to直接翻页,flag加减页。
Expand All @@ -112,10 +114,16 @@ ModalLayer {

// 预览一页文档
function toPreview() {
running = true
if(!previewPath) return
if(previewPage < 1) previewPage = 1
if(previewPage > pageCount) previewPage = pageCount
prevConn.preview(previewPath, previewPage, password)
if(previewOCR) { // 预览OCR
ocrRunning = true
const argd = configsComp.getValueDict()
argd["tbpu.merge"] = "None" // 去除段落合并
prevConn.ocr(previewPath, previewPage, password, argd)
}
}
// 预览连接器
DocPreviewConnector {
Expand All @@ -138,6 +146,20 @@ ModalLayer {
}
}
}
// ocr预览的回调
onPreviewOcr: function(info) {
let path = info[0], page = info[1], res = info[2]
if(res.code!=100&&res.code!=101) { // 遇到异常
qmlapp.popup.message(qsTr("文档预览异常"), res.data, "error")
return
}
if(path != previewPath || page != previewPage) {
console.log("[Warning] 文档OCR预览回调不匹配")
return
}
ocrRunning = false
imgViewer.showTextBoxes(res)
}
}

contentItem: DoubleRowLayout {
Expand Down Expand Up @@ -205,8 +227,30 @@ ModalLayer {
height: 1
color: theme.coverColor4
}
Text_ {
text: qsTr("预览页面")
Row {
spacing: size_.spacing
height: size_.line
Text_ {
text: qsTr("预览页面")
anchors.verticalCenter: parent.verticalCenter
}
CheckButton {
anchors.verticalCenter: parent.verticalCenter
height: size_.line
enabledAnime: true
checked: previewOCR
onCheckedChanged: {
if(!previewOCR&&checked) {
previewOCR = true
toPreview()
}
else {
previewOCR = ocrRunning = false
}
}
text_: "OCR"
toolTip: qsTr("预览PDF时,是否预览OCR结果")
}
}
Row {
spacing: size_.spacing
Expand Down Expand Up @@ -332,6 +376,11 @@ ModalLayer {
rightItem: ImageWithIgnore {
id: imgViewer
anchors.fill: parent
// 加载中 动态图标
Loading {
visible: ocrRunning
anchors.centerIn: parent
}
}
}
}

0 comments on commit 356ebff

Please sign in to comment.