亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python二維碼生成識別實例詳解

系統 1769 0

前言

在 JavaWeb 開發中,一般使用 Zxing 來生成和識別二維碼,但是,Zxing 的識別有點差強人意,不少相對模糊的二維碼識別率很低。不過就最新版本的測試來說,識別率有了現顯著提高。

對比

在沒接觸 Python 之前,曾使用 Zbar 的客戶端進行識別,測了大概幾百張相對模糊的圖片,Zbar的識別速度要快很多,識別率也比 Zxing 稍微準確那邊一丟丟,但是,稍微模糊一點就無法識別。相比之下,微信和支付寶的識別效果就逆天了。

代碼案例

            
# -*- coding:utf-8 -*-
import os
import qrcode
import time
from PIL import Image
from pyzbar import pyzbar

"""
# 升級 pip 并安裝第三方庫
pip install -U pip
pip install Pillow
pip install pyzbar
pip install qrcode
"""


def make_qr_code_easy(content, save_path=None):
  """
  Generate QR Code by default
  :param content: The content encoded in QR Codeparams
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  """
  img = qrcode.make(data=content)
  if save_path:
    img.save(save_path)
  else:
    img.show()


def make_qr_code(content, save_path=None):
  """
  Generate QR Code by given params
  :param content: The content encoded in QR Code
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  """
  qr_code_maker = qrcode.QRCode(version=2,
                 error_correction=qrcode.constants.ERROR_CORRECT_M,
                 box_size=8,
                 border=1,
                 )
  qr_code_maker.add_data(data=content)
  qr_code_maker.make(fit=True)
  img = qr_code_maker.make_image(fill_color="black", back_color="white")
  if save_path:
    img.save(save_path)
  else:
    img.show()


def make_qr_code_with_icon(content, icon_path, save_path=None):
  """
  Generate QR Code with an icon in the center
  :param content: The content encoded in QR Code
  :param icon_path: The path of icon image
  :param save_path: The path where the generated QR Code image will be saved in.
           If the path is not given the image will be opened by default.
  :exception FileExistsError: If the given icon_path is not exist.
                This error will be raised.
  :return:
  """
  if not os.path.exists(icon_path):
    raise FileExistsError(icon_path)

  # First, generate an usual QR Code image
  qr_code_maker = qrcode.QRCode(version=4,
                 error_correction=qrcode.constants.ERROR_CORRECT_H,
                 box_size=8,
                 border=1,
                 )
  qr_code_maker.add_data(data=content)
  qr_code_maker.make(fit=True)
  qr_code_img = qr_code_maker.make_image(fill_color="black", back_color="white").convert('RGBA')

  # Second, load icon image and resize it
  icon_img = Image.open(icon_path)
  code_width, code_height = qr_code_img.size
  icon_img = icon_img.resize((code_width // 4, code_height // 4), Image.ANTIALIAS)

  # Last, add the icon to original QR Code
  qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))

  if save_path:
    qr_code_img.save(save_path)
  else:
    qr_code_img.show()


def decode_qr_code(code_img_path):
  """
  Decode the given QR Code image, and return the content
  :param code_img_path: The path of QR Code image.
  :exception FileExistsError: If the given code_img_path is not exist.
                This error will be raised.
  :return: The list of decoded objects
  """
  if not os.path.exists(code_img_path):
    raise FileExistsError(code_img_path)

  # Here, set only recognize QR Code and ignore other type of code
  return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE], scan_locations=True)


if __name__ == "__main__":

  # # 簡易版
  # make_qr_code_easy("make_qr_code_easy", "make_qr_code_easy.png")
  # results = decode_qr_code("make_qr_code_easy.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")
  #
  # # 參數版
  # make_qr_code("make_qr_code", "make_qr_code.png")
  # results = decode_qr_code("make_qr_code.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")
  #
  # 帶中間 logo 的
  # make_qr_code_with_icon("https://blog.52itstyle.vip", "icon.jpg", "make_qr_code_with_icon.png")
  # results = decode_qr_code("make_qr_code_with_icon.png")
  # if len(results):
  #   print(results[0].data.decode("utf-8"))
  # else:
  #   print("Can not recognize.")

  # 識別答題卡二維碼 16 識別失敗
  t1 = time.time()
  count = 0
  for i in range(1, 33):
    results = decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png")
    if len(results):
      print(results[0].data.decode("utf-8"))
    else:
      print("Can not recognize.")
      count += 1
  t2 = time.time()
  print("識別失敗數量:" + str(count))
  print("測試時間:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))
          

測試了32張精挑細選的模糊二維碼:

            
識別失敗數量:1
測試時間:130
          

使用最新版的 Zxing 識別失敗了三張。

源碼

https://gitee.com/52itstyle/Python/tree/master/Day13

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91视频欧美| 天天操夜夜操免费视频 | 800玖玖爱在线观看香蕉 | 深夜视频在线免费 | 成人禁在线观看午夜亚洲 | 韩国精品欧美一区二区三区 | 成人网影 | 米奇精品一区二区三区 | 青青影院一区二区免费视频 | 99爱视频在线观看 | 亚洲欧美乱综合图片区小说区 | 欧美视频一区二区专区 | 久久久无码精品亚洲日韩按摩 | 亚洲成人一区 | 蜜桃精品免费久久久久影院 | 91av最新地址 | 久久精品免费 | 搡的我好爽视频在线观看 | 狠狠插天天干 | 999精品视频这里只有精品 | 911精品国产亚洲日本美国韩国 | 国产综合一区二区 | 亚洲国产精品成人综合色在线婷婷 | 久久99精品久久久久久三级 | 成人免费视频一区二区 | 日本一级在线播放线观看视频 | 一级一级一级毛片免费毛片 | 免费a级毛片大学生免费观看 | 亚洲一级黄色大片 | 欧美色精品天天在线观看视频 | 成人黄色网 | 日韩欧美高清 | 国产大毛片 | 激情五月色播 | 日韩精品欧美成人 | 奇米影视第四色在线观看 | 国产精品9999久久久久 | 亚洲狠狠婷婷综合久久久久 | 奇米婷婷 | 一区二区三区在线视频观看 | 国产羞羞视频在线播放 |