python做一些计算比较方便,用python作为程序入口,然后启动引擎,再掉接口做数据数据分析
python启动minigui引擎import sys
import os
import ctypes
import traceback
import threading
from hiktool import HikToolException
# initAppSystemFont初始化的字号
font_map = {
'FONT_INVALID':-1,
'FONT_BULLET_R_16':6,
'FONT_DEFT_R_16':7,
'FONT_CAPTION_R_17':8,
'FONT_CAPTION_B_18':9,
'FONT_SIZE_R_19':10,
'FONT_WEEK_R_20':11,
'FONT_PSWD_B_20':12,
'FONT_SIZE_R_21':13,
'FONT_TIPS_B_22':14,
}
lang_map = {
'LANG_SIMP_CN' : 0, # 简体中文 #
'LANG_ENGLISH' : 1, # 英语 #
'LANG_TRAD_CN' : 2, # 繁体中文 #
}
class SIZE(ctypes.Structure):
_fields_ = [('cx', ctypes.c_long), ('cy', ctypes.c_long)]
SYSLOGFONT_CONTROL = 5
class MiniguiEngine(threading.Thread):
def __init__(self, callback, args, **kwargs):
threading.Thread.__init__(self)
self.__callback = callback
self.__args = args
self.__kwargs = kwargs
self.lib = None
self.HWND_DESKTOP = None
self.__lang = self.__kwargs.get('lang') or 'LANG_ENGLISH'
# init selfdefine logic font
if not lang_map.get(self.__lang):
raise HikToolException('initAppSystemFont failed! Unknow lang "{}"'.format(self.__lang))
def run(self):
lib = ctypes.cdll.LoadLibrary('libsuchp.so')
self.lib = lib
#pchar_array1 = ctypes.c_char_p*1
#argv = pchar_array1()
#argv.value = b'minigui'
pchar_array1 = (ctypes.c_char*10)*1
argv = pchar_array1()
argv[0].value = b'minigui'
if lib.InitGUI(1, argv) != 0:
return -1
# get HWND_DESKTOP handle
self.HWND_DESKTOP = ctypes.c_uint.in_dll(lib, '__mg_hwnd_desktop')
print(self.HWND_DESKTOP, type(self.HWND_DESKTOP))
# for debug
lib.sysres_get_system_res_path.restype = ctypes.c_char_p
resinfo_path = lib.sysres_get_system_res_path()
print('load resource path:', resinfo_path)
lib.initAppSystemFont(lang_map[self.__lang])
ret = 0
try:
# run callback function
ret = self.__callback(self, self.__args)
except Exception as e:
ret = -1
print(traceback.print_exc())
finally:
lib.PostQuitMessage(self.HWND_DESKTOP)
lib.TerminateGUI(0)
return ret
def getTextExtentPoint(self, hdc, s):
lib = self.lib
size = SIZE(0, 0)
lib.GetTextExtentPoint(hdc, s, len(s),
-1, None, None, None, ctypes.byref(size))
return {'cw' : size.cx, 'ch' : size.cy}
def set_font(self, hdc, font):
lib = self.lib
pfont = None
if not font:
pfont = lib.getSystemFont(SYSLOGFONT_CONTROL)
elif font_map.get(font):
pfont = lib.getSystemComFont(font_map[font])
else:
raise HikToolException('Minigui engine not support font type:', font)
poldfont = self.set_font_dc(hdc, pfont)
return poldfont
def set_font_dc(self, hdc, pfont):
lib = self.lib
poldfont = lib.SelectFont(hdc, pfont);
return poldfont
def get_string_pixels(self, s, font, encoding):
lib = self.lib
hdc = lib.GetClientDC(self.HWND_DESKTOP)
poldfont = self.set_font(hdc, font)
size = self.getTextExtentPoint(hdc, s.encode(encoding))
self.set_font_dc(hdc, poldfont)
return size
def get_strings_pixels(self, strs, font, encoding):
lib = self.lib
hdc = lib.GetClientDC(self.HWND_DESKTOP)
poldfont = self.set_font(hdc, font)
if type(strs) != type([]):
raise HikToolException('get_strings_pixels argument "1" required type list:')
sizes = []
for s in strs:
size = self.getTextExtentPoint(hdc, s.encode(encoding))
sizes.append(size)
lib.set_font_dc(hdc, poldfont)
return sizes
# 计算字符串宽度的demo callback
def _call_minigui(engine, args):
strs_couple = [
('中文字符串的像素宽度', 'FONT_DEFT_R_16'),
('中文字符串的像素宽度。。', 'FONT_CAPTION_B_18'),
('中文字符串的像素宽度。。。', 'FONT_CAPTION_B_18'),
]
for couple in strs_couple:
size = engine.get_string_pixels(couple[0], couple[1], 'utf-8')
print('string="{}", font="{}", cx={}, cy={}'.format(couple[0], couple[1], size['cx'], size['cy']))
if __name__ == '__main__':
cb = _call_minigui
miniguiengine = MiniguiEngine(cb, None, lang='LANG_SIMP_CN')
miniguiengine.start()
ret = miniguiengine.join()
sys.exit(ret)



