基于Python实现二维码编码、解码、定位

给朋友做个小功能的Demo,也是参考了不少网友的思路,也不敝帚自珍啦,有用的直接拿去。

看图说话

基于Python实现二维码编码、解码、定位_第1张图片

Show me the code


import cv2
import numpy as np
import qrcode  # 安装二维码生成包 pip install qrcode
from pyzbar.pyzbar import decode    # 安装二维码解码包  pip install pyzbar


def barcode_create(data_str):
    image = qrcode.make(data_str)
    image.save('./hello.png')


def qrcode_decode(src_img):
    gray_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2GRAY)
    # Find barcodes and QR codes
    barcode_objects = decode(gray_img)

    if len(barcode_objects) > 0:
        for obj in barcode_objects:
            print(obj)

            for j in range(0, 4):
                cv2.line(src_img, obj.polygon[j], obj.polygon[(j + 1) % 4], (255, 0, 0), 3)

        cv2.putText(src_img, barcode_objects[0][0].decode("utf-8"), (20, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))


if __name__ == '__main__':
    barcode_create('hello world!-20201226')
    capture = cv2.VideoCapture(0)  # 0是代表摄像头编号,只有一个的话默认为0

    while True:
        # 调用摄像机
        ref, capframe = capture.read()
        if ref is True:
            # 二维码识别
            qrcode_decode(capframe)

            cv2.imshow('frame', capframe)
            if cv2.waitKey(15) == 'q':
                break

    capture.release()
    cv2.destroyAllWindows()

 

你可能感兴趣的:(Python,CV,编程开发)