10-3ReadQRCode.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: img = cv2.imread(‘qr_code.png’)
005:
006: height, width, _ = img.shape
007: baseImg = createBaseImg(height, width)
008: baseImg[10:height + 10, 10:width + 10] = img
009:
010: # ここで処理を実行する
011: code = cv2.QRCodeDetector()
012: data, point, qrcode = code.detectAndDecode(img=baseImg)
013:
014: print(‘DATA = {0}’.format(data))
015:
016: cv2.imshow(‘QR CODE’, baseImg)
017: cv2.waitKey(0)
018: cv2.destroyAllWindows()
019: return 0
020:
021: def createBaseImg(height, width):
022:
023: # ベース画像
024: size = np.array([height + 20, width + 20, 3])
025: whiteColor = np.array([255., 255., 255.])
026: baseimg = np.full(size, whiteColor, dtype=np.uint8)
027:
028: return baseimg
029:
030: if __name__ == ‘__main__’:
031: print(cv2.__version__)
032:
033: __main()