文章目录
import os
import sys
import qrcode
import base64
def_unit_len = 2330
def base64_file(src_file_path=''):
s_file = open(src_file_path, "rb").read()
encode_str = base64.b64encode(s_file)
res_str = str(encode_str, "utf-8")
return res_str
def len_split(text_sum_len=1, unit_len=def_unit_len) -> list:
r"""
将文本总长度根据 unit_len 切成下标数组
:param text_sum_len: 文本总长度
:param unit_len: 单位长度;每个文本片段的长度
:return: list
"""
result_list = []
count = int(text_sum_len / unit_len)
if text_sum_len % unit_len != 0:
count += 1
for i in range(0, count):
current_idx = i * unit_len
result_list.append(current_idx)
return result_list
def text_split_make_img(text=''):
r"""
文本拆分
:return:
"""
unit_len = def_unit_len
text_len = len(text)
print(text_len)
len_arr = len_split(text_len, )
print(len_arr)
for i in len_arr:
print(i)
if i + unit_len <= text_len:
tmp_txt = text[i: i + unit_len]
else:
tmp_txt = text[i:]
print(tmp_txt)
img = qrcode.make(tmp_txt)
img.save(fr'{qr_code_dir}/{i}.png')
def is_blank(text: str) -> bool:
if text is None:
return True
if len(text) <= 0:
return True
return False
sys_argv = sys.argv[1:]
qr_code_dir = r'P:/workspace/qr_code_dir/' if len(sys_argv) <= 0 or is_blank(sys_argv[1]) else sys_argv[1]
path_prefix = r'P:/workspace/pycharm_u/res/'
zip_file = path_prefix + 'pack.zip' if len(sys_argv) <= 0 or is_blank(sys_argv[0]) else sys_argv[0]
if __name__ == '__main__':
print(fr'use like: file_qrcode $zip_file $qr_code_dir ')
print(fr'example : file_qrcode "P:/workspace/pack.zip" "P:/workspace/qr_code_dir/" ')
print(fr'default : zip_file="P:/workspace/pack.zip" ; qr_code_dir="P:/workspace/qr_code_dir/" ')
print(fr'---------------------------------------------------- ')
print(fr'==>> zip_file=[{zip_file}]')
print(fr'==>> qr_code_dir=[{qr_code_dir}]')
text_str = base64_file(zip_file)
text_split_make_img(text_str)
open_target_dir = qr_code_dir.replace('/', '\\')
os.system(rf"start explorer {open_target_dir}")
from PIL import ImageGrab
from uiautomation import uiautomation as uia
def shot(idx=0):
im = ImageGrab.grab(bbox=(963, 28, 1908, 982))
im.save(rf'D:/workspace/qr_code_dir/src_png/shot_{idx}.png')
count = 3
x = 1485
y = 995
def main():
uia.Click(x, y, 1)
for i in range(0, count):
shot(i)
uia.Click(x, y, 1)
print(f'==>>: idx={i}')
if __name__ == '__main__':
main()
import os
import sys
import cv2 as cv
import pyzbar.pyzbar as pyzbar
from pyzbar import pyzbar as pyzbar
from PIL import Image, ImageEnhance
import date_util as date_util
import base64_file as base64_file
from os import listdir,system
def decode_display(image):
barcodes = pyzbar.decode(image)
for barcode in barcodes:
(x, y, w, h) = barcode.rect
cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
barcodeData = barcode.data.decode("UTF8")
barcodeType = barcode.type
text = "{} ({})".format(barcodeData, barcodeType)
cv.putText(image, text, (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 125), 2)
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
return image
def detect():
camera = cv.VideoCapture(0)
while True:
ret, frame = camera.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
im = decode_display(gray)
c = cv.waitKey(5)
cv.imshow("camera", im)
if (c == 27):
camera.release()
cv.destroyAllWindows()
break
def pil_read(image=''):
img = Image.open(image)
barcodes = pyzbar.decode(img)
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
print(barcodeData)
return barcodeData
def append_file(content):
f = open(base64_str_file, 'a')
f.write(content)
f.close()
def get_idx(file_name) -> str:
prefix = 'shot_'
begin = file_name.index(prefix) + len(prefix)
end = file_name.index('.png')
return file_name[begin:end]
qr_prefix = r'D:/workspace/qr_code_dir/'
pack_str_file = date_util.fmt_second()
base64_str_file = rf'{qr_prefix}{pack_str_file}.txt'
dist_file = rf'{qr_prefix}{pack_str_file}.zip'
def main():
prefix = r'D:/workspace/qr_code_dir/src_png/'
file_list = listdir(prefix)
print(file_list)
file_list = sorted(file_list, key=lambda file: int(get_idx(file)))
if len(file_list) <= 0:
print(fr'截屏目录为空')
return
for file_path in file_list:
print(file_path)
append_file(pil_read(prefix + file_path))
base64_file.de_base64_file(base64_str_file, dist_file)
if __name__ == '__main__':
main()
open_dir = qr_prefix.replace('/', '\\')
os.system(rf'start explorer {open_dir}')