Skeleton QT app by QT creator.
[toast/barcode2cash.git] / read_receipt_example.py
1 #!/usr/bin/python3
2 import datetime
3 import numpy as np
4 import zbar # pip install zbar-py
5 import PIL.Image
6
7 def cashstr_to_cent(cashstr):
8     return int(cashstr.replace(',', ''))
9
10 image_color = PIL.Image.open('examples/receipt01.jpg')
11 image_bw = image_color.convert('L')  # monochrome
12 pixels = np.array(image_bw)
13
14 scanner = zbar.Scanner()
15 scan_results = scanner.scan(pixels)
16 for scan_result in scan_results:
17     # https://github.com/a-sit-plus/at-registrierkassen-mustercode/releases/download/1.2-DOK/2016-09-05-Detailfragen-RKSV-V1.2.pdf
18     data_split = [str(d, 'UTF-8') for d in scan_result.data.split(b'_')]  # split and decode UTF-8 to string
19     _, r1at, device_id, receipt_id, date_time, cash0, cash1, cash2, cash3, cash4, counter, cert_serial, sig_prev, sig = data_split
20     date_time = datetime.datetime.strptime(date_time, '%Y-%m-%dT%H:%M:%S')
21     cash_cent = [cashstr_to_cent(cash) for cash in (cash0, cash1, cash2, cash3, cash4)]
22     print('Receipt')
23     print('* Date: {}'.format(date_time))
24     print('* Device ID: {}'.format(device_id))
25     print('* Receipt ID: {}'.format(receipt_id))
26     for i, cent in enumerate(cash_cent):
27         print('* Amount #{}: {} cent'.format(i+1, cent))
28     print('* Total amount: {} cent'.format(sum(cash_cent)))
29