python服务程序(调用摄像头查看视频和识别二维码)+ Android(安卓)客户端程序
16lz
2021-01-26
运行效果图
服务器端程序
#!/usr/bin/python'''服务器端程序 do_GET() 方法调用webcam摄像头,查看监控视频,同时识别二维码,do_POST()方法连接Mysql数据库,获取数据,并以webservice的形式发布出去供移动客户端与Mysql数据交互。'''import cv2import Imageimport threadingfrom BaseHTTPServer import BaseHTTPRequestHandler,HTTPServerfrom SocketServer import ThreadingMixInimport StringIOimport timeimport qrtoolsimport serialimport MySQLdbimport datetimeimport cgiimport randomimport stringcapture = Noneqr = qrtools.QR()sr = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1)db = MySQLdb.connect('localhost', 'root', 'root', 'homedb')cursor = db.cursor()def passwd_generator(): s = string.ascii_letters + string.digits l = len(s) passwd_l = 10 return ''.join([s[random.randrange(0, l-1)] for x in range(passwd_l)])class CamHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path.endswith('.mjpg'): self.send_response(200) self.send_header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary') self.end_headers() while True: try: rc, img = capture.read() if not rc: continue imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) jpg = Image.fromarray(imgRGB) tmpFile = StringIO.StringIO() jpg.save(tmpFile,'JPEG') if qr.decode(tmpFile): print(qr.data) ''' Verify QR code ''' username, passwd = qr.data.trim().split(',') cursor.execute('select * from user where username="' + qr.username + '"') data = cursor.fetchone() if not data: continue d = datetime.datetime.now() - data[3] if d < datetime.delta(seconds=10): if passwd == data[2]: ''' Open lock via arduino''' if sr.isOpen(): sr.write(b'1') self.wfile.write("--jpgboundary") self.send_header('Content-type','image/jpeg') self.send_header('Content-length',str(tmpFile.len)) self.end_headers() jpg.save(self.wfile,'JPEG') time.sleep(0.05) except KeyboardInterrupt: break return if self.path.endswith('.html'): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() with open('front.html', 'r') as f: self.wfile.write(f.read()); # self.wfile.write('') self.wfile.write('') # self.wfile.write('') with open('end.html', 'r') as f: self.wfile.write(f.read()) return def do_POST(self): if self.path == '/authenticate': ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.getheader('content-length')) postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} if postvars: username = postvars['username'][0] print("username=" + username) cursor.execute('select * from user where username=%s', (username, )) d = cursor.fetchone() if d: new_passwd = passwd_generator() print("new passwd=" + new_passwd) # cursor.execute('select * from user') cursor.execute('update user set passwd=%s, last_update=%s where username=%s', (new_passwd, datetime.datetime.now(), username)) db.commit() if cursor: self.send_response(200) self.end_headers() self.wfile.write(username + ',' + new_passwd) print("response OK!") return else: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write('Hello world