26_internetCamera.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : pi
  4. from maix import image #引入python模块包
  5. from maix import camera, mjpg, utils, display
  6. import time
  7. import qrcode
  8. import sys
  9. import os
  10. import socket
  11. sys.path.append('/root/')
  12. from CocoPi import BUTTON
  13. def getWifiConnectState():
  14. wifiConnectState=False
  15. cmd = "wifi_get_connection_info_test 1"
  16. res = os.popen(cmd).read()
  17. if res.find('get connection infomation successfully!') != -1:
  18. wifiConnectState = True
  19. else:
  20. wifiConnectState = False
  21. return wifiConnectState
  22. def getPrivateIp():
  23. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  24. try:
  25. st.connect(("10.255.255.255",1))
  26. IP = st.getsockname()[0]
  27. except Exception:
  28. IP = "127.0.0.1"
  29. finally:
  30. st.close()
  31. return IP
  32. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  33. PrivateIP=""
  34. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  35. canvas.draw_string(8 , 100 , "检查网络连接状态...", scale = 1, color = (255,255,255), thickness = 1)
  36. display.show(canvas)
  37. wirelessTransmitPath=" "
  38. try:
  39. if getWifiConnectState()==True:
  40. PrivateIP=getPrivateIp()
  41. pathHead="http://"
  42. pathTail=":18811"
  43. wirelessTransmitPath=pathHead+PrivateIP+pathTail
  44. else:
  45. PrivateIP="N/A"
  46. wirelessTransmitPath="请先连接网络"
  47. except:
  48. wirelessTransmitPath="请先连接网络"
  49. pass
  50. os.system("rm /root/user/img/wirelessPictureTransmit.jpg")
  51. qrcode.make(wirelessTransmitPath).save("/root/user/img/wirelessPictureTransmit.jpg")
  52. key_A = BUTTON(14)
  53. key_B = BUTTON(8)
  54. key_C = BUTTON(13)
  55. key_D = BUTTON(7)
  56. buttonState=0
  57. newState=0
  58. oldState=0
  59. def buttonDetect():
  60. global buttonState,newState,oldState
  61. newState=key_C.is_pressed()
  62. if newState == True and oldState == False:
  63. buttonState=1
  64. elif newState == False and oldState == True:
  65. buttonState=2
  66. else:
  67. buttonState=0
  68. oldState=newState
  69. queue = mjpg.Queue(maxsize=8)
  70. mjpg.MjpgServerThread(
  71. "0.0.0.0", 18811, mjpg.BytesImageHandlerFactory(q=queue)).start()
  72. camera.camera.config(size=(320, 240))
  73. qrShowState=0
  74. while True:
  75. buttonDetect()
  76. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  77. camera_img = camera.capture() #从摄像头中获取一张图像
  78. camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  79. jpg = utils.rgb2jpg(camera_img.convert("RGB").tobytes(), 320, 240)
  80. queue.put(mjpg.BytesImage(jpg))
  81. img_backdrop.draw_image(camera_img)
  82. if buttonState==2:
  83. qrShowState=1-qrShowState
  84. if qrShowState==1:
  85. img_backdrop.draw_image(image.open("/root/user/img/wirelessPictureTransmit.jpg").resize(260,260), 30, -10,alpha=0.8)
  86. img_backdrop.draw_image((image.open("/root/preset/img/qrcode_3c83f0_24x24.png")).rotate(0, adjust=0),288,6,alpha=1)
  87. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  88. display.show(img_backdrop) #将图像显示出来