#!/usr/bin/env python
#version    :       2024.03.14
#language   :       ch
#camera     :       CR5205

from maix import camera
from maix import display
from maix import image     #引入python模块包
import time
import sys
sys.path.append('/root/')
from CocoPi import BUTTON
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
_canvas_y,_canvas_x = 0,0
def v831_display_show_canvas(displayShow):
    global _canvas_y,_canvas_x,ScreenOrientation,cameraSize
    if ScreenOrientation:
        displayShowCanvas = image.new(size = (240, 320))
        displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
        displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
        displayShowVER = displayShowCanvas.crop(0,0,240,320)
        displayShowVER = displayShowVER.rotate(-90, adjust=1)
        display.show(displayShowVER)
    else:
        displayShowCanvas = image.new(size = (320, 240))
        displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
        displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
        display.show(displayShowCanvas)

   
image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
buttonState=0
newState=0
oldState=0
def buttonDetect():
    global buttonState,newState,oldState
    newState=key_C.is_pressed()
    if newState == True and oldState == False:
        buttonState=1
    elif newState == False and oldState == True:
        buttonState=2
    else:
        buttonState=0
    oldState=newState
    
def timeStampConvert():
    timeListOutput=""
    timeListOutput = str(time.localtime().tm_year) + \
        str(time.localtime().tm_mon)+str(time.localtime().tm_mday) + \
        str(time.localtime().tm_hour)+str(time.localtime().tm_min) + \
        str(time.localtime().tm_sec)
    return timeListOutput

key_C = BUTTON(13)
if cameraSize==True:
    camera.camera.config(size=(320,240))
else:
    camera.camera.config(size=(240,320))
while True:
    buttonDetect()
    savePathHead="/root/user/img/photo_"
    savePathTail=".jpg"
    if buttonState!=2:
        img_backdrop = image.new(size=(320,240),color=(0, 0, 0))    #创建背景画布
        camera_img = getLcdRotation(camera.capture())    #从摄像头中获取一张图像
        #camera_img_capture = camera_img.crop(0, 0, 320, 240)        #截取图像
        img_backdrop.draw_image(camera_img)
        img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
        img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        v831_display_show_canvas(img_backdrop)         #将图像显示出来
    else:
        img_backdrop = image.new(size=(320,240),color=(0, 0, 0))    #创建背景画布
        camera_img = getLcdRotation(camera.capture())    #从摄像头中获取一张图像
        camera_img_capture = camera_img.crop(0, 0, 320, 240)        #截取图像
        # camera_img_rotate=camera_img_capture.rotate(90,adjust=1)
        camera_img_capture.save(savePathHead+timeStampConvert()+savePathTail)
        img_backdrop.draw_image(camera_img_capture.resize(280, 210),20,15)
        img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
        img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        v831_display_show_canvas(img_backdrop)         #将图像显示出来
        time.sleep(0.5)
        