01_camera.py 2.5 KB

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