由于要在SAP ABAP中调用QRCODE。无法实现。怎么办呢?
那么想到了胶水Python在windows本地来实现。
#!/usr/bin/python
# *-* coding:utf-8 *-*
import sys,os, getopt
from time import sleep
from PIL import Image
import qrcode
sys.path.append(os.getcwd()) # 把当前路径(即程序所在路径)暂时加入系统的path变量中
sys.path.append("C:\Windows\System32")
sys.path.append("C:\Windows\System32\tcl")
sys.path.append("C:\Windows\SysWOW64")
sys.path.append("C:\Windows\SysWOW64\tcl")
def main(argv):
inputfile = ""
outputfile = ""
try:
# 这里的 h 就表示该选项无参数,i:表示 i 选项后需要有参数
opts, args = getopt.getopt(argv, "[i:]o:s:",[["input_photo="],"output_photo=","str="])
print("args",args)
print("opts",argv)
print("argv",argv)
except getopt.GetoptError:
print("except")
sys.exit(3)
#//遍历参数
dataStr = ""
dataInput_photo_path = ""
dataOutput_photo_path = ""
for opt, arg in opts:
if opt in ("-s", "--str"):
dataStr = arg
elif opt in ("-i", "--input_photo"):
dataInput_photo_path = arg
elif opt in ("-o", "--output_photo"):
dataOutput_photo_path = arg
if dataStr == "":
print("misssing parameter -s")
elif dataOutput_photo_path == "" :
print("misssing parameter -o ")
else:
print("parameter ok")
if dataInput_photo_path =="":
list_result = argv[3:]
else:
list_result = argv[5:]
print (list_result)
dataStr = " ".join(list_result)
print("InputedText",dataStr)
#最小尺寸 1 会生成 21 * 21 的二维码,version 每增加 1,生成的二维码就会添加 4 尺寸
#参数 error_correction 指定二维码的容错系数,分别有以下4个系数:
#ERROR_CORRECT_L: 7%的字码可被容错
#ERROR_CORRECT_M: 15%的字码可被容错
#ERROR_CORRECT_Q: 25%的字码可被容错
#ERROR_CORRECT_H: 30%的字码可被容错
#参数 box_size 表示二维码里每个格子的像素大小
#参数 border 表示边框的格子厚度是多少(默认是4)
qr = qrcode.QRCode
qr = qrcode.QRCode(version=3,
box_size=20,
border=0,
error_correction=qrcode.constants.ERROR_CORRECT_L
)
qr.add_data(dataStr)
qr.make(fit=True)
img = qr.make_image()
img = img.convert('RGBA')
if dataInput_photo_path == "":
a = 1
else:
a = 0
try:
icon = Image.open(dataInput_photo_path)
except FileNotFoundError:
print("input image not ok")
sys.exit()
icon = icon.convert('RGBA')
w, h = img.size
factor = 4
size_w = int(w/factor)
size_h = int(h/factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((w-icon_w)/2)
h = int((h-icon_h)/2)
img.paste(icon, (w, h), icon)
try:
img.save(dataOutput_photo_path, quality=100)
sleep(3)
except FileNotFoundError:
print("output image full path not ok")
sys.exit()
if __name__ == "__main__":
main(sys.argv[1:])
效果还是不错的。
缺点就是.EXE文件不能够生成1个。而是三个文件。有点多的样子。不过放到sap服务器上面就会要好得多了。