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

Python二維碼生成識(shí)別實(shí)例詳解

系統(tǒng) 1960 0

前言

在 JavaWeb 開發(fā)中,一般使用 Zxing 來(lái)生成和識(shí)別二維碼,但是,Zxing 的識(shí)別有點(diǎn)差強(qiáng)人意,不少相對(duì)模糊的二維碼識(shí)別率很低。不過(guò)就最新版本的測(cè)試來(lái)說(shuō),識(shí)別率有了現(xiàn)顯著提高。

對(duì)比

在沒(méi)接觸 Python 之前,曾使用 Zbar 的客戶端進(jìn)行識(shí)別,測(cè)了大概幾百?gòu)埾鄬?duì)模糊的圖片,Zbar的識(shí)別速度要快很多,識(shí)別率也比 Zxing 稍微準(zhǔn)確那邊一丟丟,但是,稍微模糊一點(diǎn)就無(wú)法識(shí)別。相比之下,微信和支付寶的識(shí)別效果就逆天了。

代碼案例

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

"""
# 升級(jí) pip 并安裝第三方庫(kù)
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__":

  # # 簡(jiǎn)易版
  # 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.")
  #
  # # 參數(shù)版
  # 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.")

  # 識(shí)別答題卡二維碼 16 識(shí)別失敗
  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("識(shí)別失敗數(shù)量:" + str(count))
  print("測(cè)試時(shí)間:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))
          

測(cè)試了32張精挑細(xì)選的模糊二維碼:

            
識(shí)別失敗數(shù)量:1
測(cè)試時(shí)間:130
          

使用最新版的 Zxing 識(shí)別失敗了三張。

源碼

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产九九免费视频网站 | 午夜体验 | 国产亚洲精品久久久久久久软件 | 午夜二级 | 亚洲三级天堂 | 国产成人精品亚洲日本在线观看 | 亚洲国产精品第一区二区 | 完整日本特级毛片 | 暗香影院午夜国产精品 | 亚洲一级毛片在线观 | 精品一久久香蕉国产线看播放 | 国产精品久久久久久久福利院 | 欧美日韩成人在线观看 | 亚洲视频污| 精品四虎免费观看国产高清午夜 | 成人欧美一区二区三区视频不卡 | 狠狠色噜噜狠狠狠 | 亚洲欧美不卡中文字幕 | 免费aa毛片| 一个色的综合 | 国产在线色视频 | 国产亚洲美女精品久久久 | 午夜视频www | 天天爽夜夜爽夜夜爽精品视频 | 伊人热人久久中文字幕 | 亚洲国产综合在线 | 春色www在线视频观看 | 劲爆激情欧美毛片 | 一级黄片一级毛片 | 97色在线观看免费视频 | 久久国产精品成人免费 | 久久高清精品 | 国产精品永久免费10000 | 国产一级内谢a级高清毛片 国产一级片毛片 | 手机看片欧美日韩 | 五月桃花网婷婷亚洲综合 | 中文字幕亚洲综合久久菠萝蜜 | 免费 黄 色 人成 视频 | 久久99精品国产一区二区三区 | 国产精品a人片在线观看 | 日韩精品久久久毛片一区二区 |