Skip to content

Commit

Permalink
新功能:生成二维码
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Nov 13, 2023
1 parent 42ac507 commit 34dab57
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 3 deletions.
10 changes: 7 additions & 3 deletions UmiOCR-data/py_src/tag_pages/QRcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ def scanPaths(self, paths, configDict):
self._simpleMission.addMissionList(msnList)

# 生成二维码
# format: "Aztec","Codabar","Code128","Code39","Code93","DataBar","DataBarExpanded","DataMatrix","EAN13","EAN8","ITF","LinearCodes","MatrixCodes","MaxiCode","MicroQRCode","NONE","PDF417","QRCode","UPCA","UPCE",
# format: "Aztec","Codabar","Code128","Code39","Code93","DataBar","DataBarExpanded","DataMatrix","EAN13","EAN8","ITF","LinearCodes","MatrixCodes","MaxiCode","MicroQRCode","PDF417","QRCode","UPCA","UPCE",
# quiet_zone: 四周的空闲区域
# ec_level:纠错等级,-1 - 自动, 1- L-7% , 0 - M-15%, 3 - Q-25%, 2 - H-30%
# 纠错仅用于Aztec、PDF417和QRCode
def writeBarcode(self, format, text, w, h, quiet_zone, ec_level):
def writeBarcode(self, format, text, w=0, h=0, quiet_zone=-1, ec_level=-1):
# 转整数
w, h = round(w), round(h)
quiet_zone, ec_level = round(quiet_zone), round(ec_level)
# 生成格式对象
bFormat = getattr(zxingcpp.BarcodeFormat, format, None)
if not bFormat:
return f"[Error] format {format} not in zxingcpp.BarcodeFormat!"
try:
bit = zxingcpp.write_barcode(bFormat, text, w, h, quiet_zone, ec_level)
except Exception as e:
return f"[Error] write_barcode: {e}"
return f"[Error] [{format}] {e}"
try:
img = Image.fromarray(bit, "L")
except Exception as e:
Expand Down
121 changes: 121 additions & 0 deletions UmiOCR-data/qt_res/qml/TabPages/QRcode/QRcode.qml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ TabPage {
Qt.callLater(()=>qmlapp.mainWin.setVisibility(true))
}

// 生成二维码
function writeBarcode(text) {
if(!text || text.length===0)
return
running = true
const configDict = configsComp.getValueDict()
const format = configDict["writeBarcode.format"]
const w = configDict["writeBarcode.width"]
const h = configDict["writeBarcode.height"]
const quiet_zone = configDict["writeBarcode.quiet_zone"]
const ec_level = configDict["writeBarcode.ec_level"]
const imgID = tabPage.callPy("writeBarcode", format, text, w, h, quiet_zone, ec_level)
running = false
if(imgID.startsWith("[Error]") || imgID.startsWith("[Warning]")) {
if(imgID.startsWith("[Error] [")) {
const msg = qsTr("参数有误,或输入内容不合规定。请参照报错指示修改:") +"\n"+ imgID
qmlapp.popup.message(qsTr("生成二维码失败"), msg, "error")
}
else {
qmlapp.popup.message(qsTr("生成二维码失败"), imgID, "error")
}
return
}
imageText.showImgID(imgID)
}

// ========================= 【python调用qml】 =========================

// 获取一个扫码的返回值
Expand Down Expand Up @@ -215,6 +241,16 @@ TabPage {
spacing: size_.smallSpacing
visible: dLeftTop.width > dLeftTopL.width + dLeftTopR.width

// 菜单
IconButton {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: height
icon_: "menu"
color: theme.subTextColor
onClicked: imageText.popupMenu()
toolTip: qsTr("右键菜单")
}
// 保存图片
IconButton {
anchors.top: parent.top
Expand Down Expand Up @@ -292,6 +328,86 @@ TabPage {
visible: false
}

//生成面板
Item {
id: writePanel
anchors.fill: parent
visible: false
Item {
id: writePanelTop
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: size_.line * 2 + size_.smallSpacing * 2
Button_ {
id: writePanelBtn1
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.margins: size_.smallSpacing
text_: qsTr("设置")
onClicked: {
tabPanel.currentIndex = 0 // 转到设置面板
configsComp.panelComponent.scrollToGroup(3) // 滚动到生成设置
}
}
Row {
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.margins: size_.smallSpacing
CheckButton {
anchors.top: parent.top
anchors.bottom: parent.bottom
text_: qsTr("自动刷新")
toolTip: qsTr("修改文字后,自动生成二维码/条形码")
visible: writePanelTop.width > writePanelBtn1.width+writePanelBtn2.width+this.width
textColor_: theme.textColor
checked: writeEdit.autoUpdate
enabledAnime: true
onCheckedChanged: {
writeEdit.autoUpdate = checked
}
}
Button_ {
id: writePanelBtn2
anchors.top: parent.top
anchors.bottom: parent.bottom
text_: qsTr("刷新")
toolTip: qsTr("生成二维码/条形码")
onClicked: writeBarcode(writeEdit.text)
}
}
}
Rectangle {
anchors.top: writePanelTop.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
color: theme.bgColor
border.width: 1
border.color: theme.coverColor4
TextEdit_ {
id: writeEdit
anchors.fill: parent
anchors.margins: size_.spacing
// 自动刷新
property bool autoUpdate: true
// 文字输入改变时,等待一段时间,自动刷新
Timer {
id: writeEditTimer
interval: 500 // 0.5 秒
repeat: false
onTriggered: writeBarcode(writeEdit.text)
}
onTextChanged: {
if(autoUpdate) // 重启计时器
writeEditTimer.restart()
}
}
}
}

tabsModel: [
{
"key": "configs",
Expand All @@ -303,6 +419,11 @@ TabPage {
"title": qsTr("记录"),
"component": resultsTableView,
},
{
"key": "writePanel",
"title": qsTr("生成"),
"component": writePanel,
},
]
}
}
Expand Down
67 changes: 67 additions & 0 deletions UmiOCR-data/qt_res/qml/TabPages/QRcode/QRcodeConfigs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,73 @@ Configs {
},
},

"writeBarcode": {
"title": qsTr("生成二维码/条形码"),
"type": "group",

"format": {
"title": qsTr("类型"),
"toolTip": qsTr("默认二维码:")+"QRCode",
"default": "QRCode",
"optionsList": [
["Aztec", "Aztec"],
["Codabar", "Codabar"],
["Code128", "Code128"],
["Code39", "Code39"],
["Code93", "Code93"],
["DataBar", "DataBar"],
["DataBarExpanded", "DataBarExpanded"],
["DataMatrix", "DataMatrix"],
["EAN13", "EAN13"],
["EAN8", "EAN8"],
["ITF", "ITF"],
["LinearCodes", "LinearCodes"],
["MatrixCodes", "MatrixCodes"],
["MaxiCode", "MaxiCode"],
["MicroQRCode", "MicroQRCode"],
["PDF417", "PDF417"],
["QRCode", "QRCode"],
["UPCA", "UPCA"],
["UPCE", "UPCE"],
],
},
"width": {
"title": qsTr("宽度"),
"toolTip": qsTr("填0:自动选择"),
"isInt": true,
"default": 128,
"min": 0,
"unit": qsTr("像素"),
},
"height": {
"title": qsTr("高度"),
"toolTip": qsTr("填0:自动选择"),
"isInt": true,
"default": 128,
"min": 0,
"unit": qsTr("像素"),
},
"quiet_zone": {
"title": qsTr("边缘空白"),
"toolTip": qsTr("填-1:自动选择"),
"isInt": true,
"default": -1,
"min": -1,
"unit": qsTr("像素"),
},
"ec_level": {
"title": qsTr("纠错等级"),
"toolTip": qsTr("仅适用于:")+"Aztec, PDF417, QRCode",
"optionsList": [
[-1, qsTr("自动")],
[1, "7%"],
[0, "15%"],
[3, "25%"],
[2, "30%"],
],
},
},

"other": {
"title": qsTr("其它"),
"type": "group",
Expand Down

0 comments on commit 34dab57

Please sign in to comment.