import requests import time import json import uuid import sys import os import threading # 获取分辨率索引 url = "http://localhost:6543/GetAllDisplayInfo" param = { "dev_idx": "0", } import inspect import ctypes # 关闭线程 def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # """if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect""" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit) class GaoPaiYi: # path = "/home/yanyi/Project/MainManage/img" # 默认存储地址 path = os.path.join(os.getcwd(), "img") if not os.path.exists(path): os.umask(0) os.makedirs(path) def __init__(self): url = "http://localhost:6543/GetAllDisplayInfo" try: res = requests.get(url=url) # 启动高拍仪 print(json.loads(res.text)) print(json.loads(res.text)['data']) print(json.loads(res.text)['code'] == '0') except: pass def openCamera(self): try: url = "http://127.0.0.1:6543/GetAllDisplayInfo" param = {} header = {'Content-Type': 'application/json;charset=utf-8'} res = requests.get(url=url, data=json.dumps(param), headers=header, timeout=10) print(json.loads(res.text)) url = 'http://127.0.0.1:6543/video=stream&camidx=0' res = requests.get(url=url, data=json.dumps(param), headers=header, timeout=10) print(json.loads(res.text)) print(json.loads(res.text)['filepath']) print('打开相机:::', json.loads(res.text)['code']) except Exception as e: print(e) return {'code': -1, 'msg': e} # win 版本 # def getPic(self): # try: # p = threading.Thread(target=self.openCamera) # p.start() # time.sleep(2) # # 摄像头拍照 # url = 'http://127.0.0.1:6543/video=grabimage' # # url = 'https://restapi.getui.com/v2/h8m8bkLRUG6C313Xw8sNV8/auth' # param = { # "filepath": f"{self.path}\\{str(uuid.uuid1())}.jpg", # "rotate": "90", #// 图像旋转角度,90的整数倍,默认:"0" # "deskew": "0", #// 纠偏(主头有效),参数:0:不纠偏;1:纠偏 # "deskewval":"0", #// 纠偏像素值:正常给0,正数时多裁,负数时少裁 # "camidx": "0", #// 摄像头索引,参数:0:主头;1:副头 # "ColorMode": "0", #// 色彩模式,图片保存本地时调用。0:彩色 1:灰色 2:黑白 3:白纸印章 4:去背景色(普通文件) 5:去背景色(身份证) # "quality": "0", #// 图片质量,图片保存本地调用 # # 。0:默认质量;1:高质量;2:较高质量;3:中质量;4:较低质量;5:低质量 # "bAutoAdjust":"1", #// 是否自动摆正: 0:不摆正 1:摆正 # "bIsPrint1to1":"", #// 是否1:1打印 # "watermark": { # "pos": "", #// 水印在图像中的位置,0:左上;1:右上;2:左下;3:右下;4:中间 # "content": "", #// 水印内容,必须utf-8编码,当水印内容为空,将当前时间作为水印 # "transparency": "", #// 透明度,0~255,0:完全透明;255:不透明 # "fontsize": "", #// 字体大小,默认:32 # "font": "", #// 字体 # "color": "" # // 水印颜色,colorname # } # } # header ={'Content-Type':'application/json;charset=utf-8'} # res = requests.post(url = url,data=json.dumps(param),headers=header) # print(json.loads(res.text).get('filepath')) # if json.loads(res.text)['code'] == "0": # res_data = {'code':0,'msg':"图片保存成功","path":json.loads(res.text)['filepath'].split("\\")[-1]} # else: # res_data= {'code':-1, 'msg':"图片保存失败", "path":json.loads(res.text)['code']} # print(res_data) # # 关闭摄像头 # url = 'http://127.0.0.1:6543/video=close' # param = {"camidx":"0"} # header = {'Content-Type': 'application/json;charset=utf-8'} # res = requests.post(url=url, data=json.dumps(param), headers=header,timeout=10) # print('关闭摄像头:::::::',json.loads(res.text)['code']) # stop_thread(thread=p) # return res_data # except Exception as e: # print('报错信息: ' + e + ' ,File: ' + __file__ + ', Line ' + str(sys._getframe().f_lineno)) # return {'code':-1,'msg':e} # # linux 版本 def getPic(self): try: # if path: # self.path = path # print('----------预览视频-------------------------') # 打開視頻 url = "http://localhost:6543/StartPreview?dev_idx=0&res_id=0&pixfmt=pixfmt" res = requests.get(url=url) print(res.text) # print(json.loads(res.text)['data']) # print(json.loads(res.text)['code']=='0') if json.loads(res.text)['returnCode'] != 0: return {'code': -1, 'msg': "打开视频失败"} time.sleep(5) # print('----------拍照-------------------------',__file__) # 打開視頻 url = "http://localhost:6543/getPic?savepath=" + self.path + "&quality=80" res = requests.get(url=url) # print(json.loads(res.text)['data']["path"]) # print(json.loads(res.text)['returnCode']==0) /home/yanyi/Project/MainManage/img/2022-9-9 15-36-35.jpg if json.loads(res.text)['returnCode'] == 0: res_data = {'code': 0, 'msg': "图片保存成功", "path": json.loads(res.text)['data']["path"].split("/")[-1]} else: res_data = {'code': -1, 'msg': "图片保存失败", "path": json.loads(res.text)['returnCode']} # print('----------关闭视频-------------------------') url = "http://localhost:6543/StopPreview?dev_idx=0" res = requests.get(url=url) return res_data except Exception as e: print('报错信息: ' + str(e) + ' ,File: ' + __file__ + ', Line ' + str(sys._getframe().f_lineno)) return {'code': -1, 'msg': "高拍仪连接失败,请打开高拍仪地址重新连接一下,{0}".format(str(e))} if __name__ == "__main__": obj = GaoPaiYi() obj.getPic() path = '/tmp' # pic_url = GaoPaiYi().getPic(path) # print(pic_url)