#!/usr/bin/env python
#version    :       2023.12.31
#language   :       ch
from maix import camera, display, zbar, image
import sys
import os
ScreenOrientation = False
try:
    if os.path.exists("/etc/cameraSize.cfg"):
        cameraSize = True
    else:
        cameraSize = False
except:
    cameraSize = False
def getLcdRotation(cameraCapture):
    global cameraSize
    if cameraSize:
        return lcdRotationNew(cameraCapture)
    else:
        return lcdRotation(cameraCapture)

def lcdRotationNew(inputImg):
    global cameraSize,ScreenOrientation
    imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
    if ScreenOrientation:
        imgRotationAim = image.new(size = (240, 320))
        rotationAngle = 90
        GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
    else:
        imgRotationAim = image.new(size = (320, 240))
        GETROTATION = imageRotationBuffer

    GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
    return GETROTATION

def lcdRotation(inputImg):
    global cameraSize,ScreenOrientation
    imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
    if ScreenOrientation:
        imgRotationAim = image.new(size = (240, 320))
        rotationAngle = 180
    else:
        imgRotationAim = image.new(size = (320, 240))
        rotationAngle = 90
    GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
    GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
    return GETROTATION
   
sys.path.append('/root/CocoPi.py')
image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
camera.camera.config(size=(240,320))

while True:
    canvas = getLcdRotation(camera.capture())
    mks = canvas.find_qrcodes()
    for mk in mks:
        #外框数据
        X = mk['x']
        Y = mk['y']
        W = mk['w']
        H = mk['h']            
        #二维码信息
        string = mk['payload']
        #画外框
        canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2) 
        #打印信息
        canvas.draw_string(int(X) , int(Y - 35) , str(string), scale = 1, color = (255, 0, 0), thickness = 2)  #内框ID
    canvas.draw_image(image.open("/root/preset/img/exit_ff0000_24x24.png"),288,216,alpha=1)
    display.show(canvas)
    
