欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现的二维码生成小软件

程序员文章站 2022-10-11 22:23:54
前几天,我估摸着做一个能生成qr code小程序,并能用wxpython在屏幕上显示出来。当然,我想用纯python实现,观望了一会后,我找到了三个候选: github...

前几天,我估摸着做一个能生成qr code小程序,并能用wxpython在屏幕上显示出来。当然,我想用纯python实现,观望了一会后,我找到了三个候选:

github 上的
sourceforge上的
goolge code 上的

我尝试了python-qrcode以及pyqrnative,因为它们能够运行在windows/mac/linux。也不需要依赖额外的其他库除了python图像库。pyqrcode项目需要其他一些先决条件,并且不能在windows上运行,所以我不想与之纠缠了。我最后使用了一些以前写过的一个photo viewer程序的代码,然后稍微地修改了一下,就成了qrcode的查看器了。

开始

正如我上面提到的,你只需要python图像库,gui部分我们将使用wxpython。python-qrcode相比pyqrnative生成图片更快,并包含了你见过的大多数qr码类型。

生成 qr codes

当你准备好所有需要的以后,你可以运行以下代码,看看python做了些啥:

import os
import wx
 
try:
  import qrcode
except importerror:
  qrcode = none
 
try:
  import pyqrnative
except importerror:
  pyqrnative = none
 
########################################################################
class qrpanel(wx.panel):
  """"""
 
  #----------------------------------------------------------------------
  def __init__(self, parent):
    """constructor"""
    wx.panel.__init__(self, parent=parent)
    self.photo_max_size = 240
    sp = wx.standardpaths.get()
    self.defaultlocation = sp.getdocumentsdir()
 
    img = wx.emptyimage(240,240)
    self.imagectrl = wx.staticbitmap(self, wx.id_any,
                     wx.bitmapfromimage(img))
 
    qrdatalbl = wx.statictext(self, label="text to turn into qr code:")
    self.qrdatatxt = wx.textctrl(self, value="http://www.mousevspython.com", size=(200,-1))
    instructions = "name qr image file"
    instructlbl = wx.statictext(self, label=instructions)
    self.qrphototxt = wx.textctrl(self, size=(200,-1))
    browsebtn = wx.button(self, label='change save location')
    browsebtn.bind(wx.evt_button, self.onbrowse)
    deflbl = "default save location: " + self.defaultlocation
    self.defaultlocationlbl = wx.statictext(self, label=deflbl)
 
    qrcodebtn = wx.button(self, label="create qr with qrcode")
    qrcodebtn.bind(wx.evt_button, self.onuseqrcode)
    pyqrnativebtn = wx.button(self, label="create qr with pyqrnative")
    pyqrnativebtn.bind(wx.evt_button, self.onusepyqr)
 
    # create sizer
    self.mainsizer = wx.boxsizer(wx.vertical)
    qrdatasizer = wx.boxsizer(wx.horizontal)
    locationsizer = wx.boxsizer(wx.horizontal)
    qrbtnsizer = wx.boxsizer(wx.vertical)
 
    qrdatasizer.add(qrdatalbl, 0, wx.all, 5)
    qrdatasizer.add(self.qrdatatxt, 1, wx.all|wx.expand, 5)
    self.mainsizer.add(wx.staticline(self, wx.id_any),
              0, wx.all|wx.expand, 5)
    self.mainsizer.add(qrdatasizer, 0, wx.expand)
    self.mainsizer.add(self.imagectrl, 0, wx.all, 5)
    locationsizer.add(instructlbl, 0, wx.all, 5)
    locationsizer.add(self.qrphototxt, 0, wx.all, 5)
    locationsizer.add(browsebtn, 0, wx.all, 5)
    self.mainsizer.add(locationsizer, 0, wx.all, 5)
    self.mainsizer.add(self.defaultlocationlbl, 0, wx.all, 5)
 
    qrbtnsizer.add(qrcodebtn, 0, wx.all, 5)
    qrbtnsizer.add(pyqrnativebtn, 0, wx.all, 5)
    self.mainsizer.add(qrbtnsizer, 0, wx.all|wx.center, 10)
 
    self.setsizer(self.mainsizer)
    self.layout()
 
  #----------------------------------------------------------------------
  def onbrowse(self, event):
    """"""
    dlg = wx.dirdialog(self, "choose a directory:",
              style=wx.dd_default_style)
    if dlg.showmodal() == wx.id_ok:
      path = dlg.getpath()
      self.defaultlocation = path
      self.defaultlocationlbl.setlabel("save location: %s" % path)
    dlg.destroy()
 
  #----------------------------------------------------------------------
  def onuseqrcode(self, event):
    """
    https://github.com/lincolnloop/python-qrcode
    """
    qr = qrcode.qrcode(version=1, box_size=10, border=4)
    qr.add_data(self.qrdatatxt.getvalue())
    qr.make(fit=true)
    x = qr.make_image()
 
    qr_file = os.path.join(self.defaultlocation, self.qrphototxt.getvalue() + ".jpg")
    img_file = open(qr_file, 'wb')
    x.save(img_file, 'jpeg')
    img_file.close()
    self.showqrcode(qr_file)
 
  #----------------------------------------------------------------------
  def onusepyqr(self, event):
    """

http://code.google.com/p/pyqrnative/

    """
    qr = pyqrnative.qrcode(20, pyqrnative.qrerrorcorrectlevel.l)
    qr.adddata(self.qrdatatxt.getvalue())
    qr.make()
    im = qr.makeimage()
 
    qr_file = os.path.join(self.defaultlocation, self.qrphototxt.getvalue() + ".jpg")
    img_file = open(qr_file, 'wb')
    im.save(img_file, 'jpeg')
    img_file.close()
    self.showqrcode(qr_file)
 
  #----------------------------------------------------------------------
  def showqrcode(self, filepath):
    """"""
    img = wx.image(filepath, wx.bitmap_type_any)
    # scale the image, preserving the aspect ratio
    w = img.getwidth()
    h = img.getheight()
    if w > h:
      neww = self.photo_max_size
      newh = self.photo_max_size * h / w
    else:
      newh = self.photo_max_size
      neww = self.photo_max_size * w / h
    img = img.scale(neww,newh)
 
    self.imagectrl.setbitmap(wx.bitmapfromimage(img))
    self.refresh()
 
 
########################################################################
class qrframe(wx.frame):
  """"""
 
  #----------------------------------------------------------------------
  def __init__(self):
    """constructor"""
    wx.frame.__init__(self, none, title="qr code viewer", size=(550,500))
    panel = qrpanel(self)
 
if __name__ == "__main__":
  app = wx.app(false)
  frame = qrframe()
  frame.show()
  app.mainloop()

python-qrcode生成效果图:

Python实现的二维码生成小软件

pyqrnative生成效果图:

Python实现的二维码生成小软件