01_camera.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. #version : 2024.03.14
  3. #language : ch
  4. #camera : CR5205
  5. from maix import camera
  6. from maix import display
  7. from maix import image #引入python模块包
  8. import time
  9. import sys
  10. sys.path.append('/root/')
  11. from CocoPi import BUTTON
  12. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  13. buttonState=0
  14. newState=0
  15. oldState=0
  16. def buttonDetect():
  17. global buttonState,newState,oldState
  18. newState=key_C.is_pressed()
  19. if newState == True and oldState == False:
  20. buttonState=1
  21. elif newState == False and oldState == True:
  22. buttonState=2
  23. else:
  24. buttonState=0
  25. oldState=newState
  26. def timeStampConvert():
  27. timeListOutput=""
  28. timeListOutput = str(time.localtime().tm_year) + \
  29. str(time.localtime().tm_mon)+str(time.localtime().tm_mday) + \
  30. str(time.localtime().tm_hour)+str(time.localtime().tm_min) + \
  31. str(time.localtime().tm_sec)
  32. return timeListOutput
  33. key_C = BUTTON(13)
  34. camera.camera.config(size=(320, 240))
  35. while True:
  36. buttonDetect()
  37. savePathHead="/root/user/img/photo_"
  38. savePathTail=".jpg"
  39. if buttonState!=2:
  40. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  41. camera_img = camera.capture() #从摄像头中获取一张图像
  42. #camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  43. img_backdrop.draw_image(camera_img)
  44. img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  45. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  46. display.show(img_backdrop) #将图像显示出来
  47. else:
  48. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  49. camera_img = camera.capture() #从摄像头中获取一张图像
  50. camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  51. # camera_img_rotate=camera_img_capture.rotate(90,adjust=1)
  52. camera_img_capture.save(savePathHead+timeStampConvert()+savePathTail)
  53. img_backdrop.draw_image(camera_img_capture.resize(280, 210),20,15)
  54. img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  55. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  56. display.show(img_backdrop) #将图像显示出来
  57. time.sleep(0.5)