An interesting question about adding QR code to odoo device and generating QR code using python
In the odoo device integration, a QR code was added according to customer requirements. If I wanted to include some information, I used python's QR code library qrcode. qrcode is a Python open source library for QR code generation .
code show as below:
ewmny = '设备:' + label.name + ',状态:' + label.zhuangtai # + ',编号:' + label.gongsibh
if label.zhuangtai:
ewmny = ewmny + ',状态:' + label.zhuangtai
else:
ewmny = ewmny + ',状态: '
if label.gongsibh:
ewmny = ewmny + ',编号:' + label.gongsibh
ewm = qrcode.make(ewmny) # 将330*330大小的二维码
# print(ewm.size)
ewm.thumbnail((80, 80))
ewmf = './tempdata/wmf.png'
ewm.save(ewmf)
After the production is displayed, the results are scanned using WeChat and the prompts are as follows:
WeChat currently does not support displaying text content in QR codes.
Remember that it could be displayed before. But there is a line under WeChat that says "Copy text content". After copying and pasting it somewhere else, you can still see the content.
It said online that you can add a picture, so I tried it.
from PIL import Image
import qrcode, os
def create_qrcode(url, qrcodename):
qr = qrcode.QRCode(
version=1, # 设置容错率为最高
error_correction=qrcode.ERROR_CORRECT_H, # 用于控制二维码的错误纠正程度
box_size=8, # 控制二维码中每个格子的像素数,默认为10
border=1, # 二维码四周留白,包含的格子数,默认为4
)
qr.add_data(url) # QRCode.add_data(data)函数添加数据
qr.make(fit=True) # QRCode.make(fit=True)函数生成图片
img = qr.make_image()
img = img.convert("RGBA") # 二维码设为彩色
logo = Image.open("images/ewm.png") # 传gif生成的二维码也是没有动态效果的
w,h = img.size
logo_w,logo_h = logo.size
l_w = int((w - logo_w) / 2)
l_h = int((h - logo_h) / 2)
logo = logo.convert("RGBA")
img.paste(logo, (l_w, l_h), logo)
img.show()
img.save(os.getcwd() + "/images/" + qrcodename + ".png", quality=100)
def main():
url = input("请输入文本或URL:")
qrcodename = input("请输入生成二维码的名称:")
create_qrcode(url,qrcodename)
if __name__ == '__main__':
main()
I scanned it with WeChat and couldn’t recognize it.