user_latest_code.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from maix import camera, display, zbar, image
  2. import socket
  3. import os
  4. import sys
  5. sys.path.append('/root/CocoPi.py')
  6. import http.client
  7. from CocoPi import BUTTON
  8. import time
  9. key_A = BUTTON(14)
  10. key_B = BUTTON(8)
  11. key_C = BUTTON(13)
  12. key_D = BUTTON(7)
  13. ScreenOrientation = False
  14. image.load_freetype("/root/preset/fonts/yahei-ttf.ttf")
  15. camera.camera.config(size=(240,320))
  16. def lcdRotation(inputImg,rotationAngle):
  17. from maix import image
  18. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  19. if ScreenOrientation:
  20. imgRotationAim = image.new(size = (240, 320))
  21. else:
  22. imgRotationAim = image.new(size = (320, 240))
  23. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  24. if ScreenOrientation:
  25. CAMERAROTATE = +180
  26. else:
  27. CAMERAROTATE = +90
  28. def getNetworkDate_noexit():
  29. global getDateNum
  30. try:
  31. coon = http.client.HTTPConnection('www.baidu.com')
  32. coon.request("GET","/")
  33. r = coon.getresponse()
  34. ts = r.getheader('date')
  35. GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
  36. BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60)
  37. format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
  38. command = "date -s "+"\"{}\"".format(format_time)
  39. os.system(command)
  40. getDateNum = 1
  41. # sys.exit()
  42. except:
  43. pass
  44. def wifi_is_content():
  45. global getDateNum
  46. cmd = "wifi_get_connection_info_test 1"
  47. res = os.popen(cmd).read()
  48. data = False
  49. print(res)
  50. if res.find('get connection infomation successfully!') != -1:
  51. data = True
  52. return data
  53. def extract_ip():
  54. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  55. try:
  56. st.connect(("10.255.255.255",1))
  57. IP = st.getsockname()[0]
  58. except Exception:
  59. IP = "127.0.0.1"
  60. finally:
  61. st.close()
  62. return IP
  63. SSID = ""
  64. PASS = ""
  65. isContent = True
  66. contentText = ""
  67. canvas1 = image.new(size = (320, 240))
  68. while True:
  69. canvas = lcdRotation(camera.capture(),CAMERAROTATE)
  70. IP = extract_ip()
  71. mks = canvas.find_qrcodes()
  72. for mk in mks:
  73. #外框数据
  74. X = mk['x']
  75. Y = mk['y']
  76. W = mk['w']
  77. H = mk['h']
  78. #二维码信息
  79. string = mk['payload']
  80. codeData = string.split(";")
  81. SSID = codeData[0].split(":")[1]
  82. PASS = codeData[1].split(":")[1]
  83. #画外框
  84. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  85. #打印信息
  86. canvas.draw_string(int(X) , int(Y - 35) , str(string), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  87. if key_C.is_pressed():
  88. while not (key_C.is_pressed() == False):
  89. time.sleep(0.01)
  90. os.system("wifi_disconnect_ap_test")
  91. os.system('wifi_connect_chinese_ap_test '+SSID+' '+PASS+'')
  92. isContent = False
  93. contentText == ""
  94. elif(contentText == "" and isContent == False):
  95. if wifi_is_content():
  96. contentText = "Wi-Fi success"
  97. canvas1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  98. else:
  99. contentText = "Wi-Fi failure"
  100. isContent = True
  101. # canvas1.draw_rectangle(0,0, 320,20, color=(255,255,255), thickness=-1)
  102. canvas.draw_image(canvas1,0,0, alpha=0.6)
  103. canvas.draw_string(10,50, contentText, scale = 1.5, color = (0,0,0) , thickness = 1)
  104. canvas.draw_string(10,0, "Press C to take pictures for networking", scale = 1, color = (0,0,0) , thickness = 1)
  105. canvas.draw_string(10,20, "Hold D to build and exit", scale = 1, color =(0,0,0) , thickness = 1)
  106. if IP != "127.0.0.1" and contentText != "":
  107. canvas.draw_string(10,80, "IP:" + extract_ip(), scale = 1.5, color = (0,0,0), thickness = 1)
  108. display.show(canvas)