|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# @Time : 2018-12-31 18:35 |
| 3 | +# @Author : play4fun |
| 4 | +# @File : QR_Scaner1.py |
| 5 | +# @Software: PyCharm |
| 6 | + |
| 7 | +""" |
| 8 | +QR_Scaner1.py: |
| 9 | +""" |
| 10 | + |
| 11 | +from pyzbar.pyzbar import decode |
| 12 | +# from PIL import Image |
| 13 | +import cv2 |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + fp = 'macbookPro.jpg' |
| 18 | + # img = Image.open(fp) |
| 19 | + # img.show() |
| 20 | + image = cv2.imread(fp) |
| 21 | + barcodes = decode(image) |
| 22 | + decoded = barcodes[0] |
| 23 | + print(decoded) |
| 24 | + # |
| 25 | + url: bytes = decoded.data |
| 26 | + url = url.decode() |
| 27 | + print(url) |
| 28 | + # rect |
| 29 | + rect = decoded.rect |
| 30 | + print(rect) # Rect(left=19, top=19, width=292, height=292) |
| 31 | + |
| 32 | + # loop over the detected barcodes |
| 33 | + for barcode in barcodes: |
| 34 | + # extract the bounding box location of the barcode and draw the |
| 35 | + # bounding box surrounding the barcode on the image |
| 36 | + (x, y, w, h) = barcode.rect |
| 37 | + cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) |
| 38 | + |
| 39 | + # the barcode data is a bytes object so if we want to draw it on |
| 40 | + # our output image we need to convert it to a string first |
| 41 | + barcodeData = barcode.data.decode("utf-8") |
| 42 | + barcodeType = barcode.type |
| 43 | + |
| 44 | + # draw the barcode data and barcode type on the image |
| 45 | + text = "{} ({})".format(barcodeData, barcodeType) |
| 46 | + cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, |
| 47 | + 0.5, (0, 0, 255), 2) |
| 48 | + |
| 49 | + # print the barcode type and data to the terminal |
| 50 | + print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData)) |
| 51 | + |
| 52 | + # show the output image |
| 53 | + cv2.imshow("Image", image) |
| 54 | + cv2.imwrite('macbook_qr_rect.jpg', image) |
| 55 | + cv2.waitKey(0) # 按任意键退出 |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + main() |
0 commit comments