UC02–不定期的记录孩子的电脑桌面
US06-按时间随机抓屏:
- 抓屏对象Capture:
(1)从【0,59】中随机采样sampling_times_minute个数字
(2)从小到大排序
(3)如果当前时间的秒大于最大的随机数,则每次休眠0.4秒,知道小于等于为止
(4)遍历随机数字,如果当前秒小于随机数,则等待0.4秒,直到相等。
(5)抓屏幕,保存为图片文件
- sampling_times_minute 每分钟采样次数
- capture() 实时不停的抓拍
- random_nums() 从【0,59】中随机采样sampling_times_minute个数字
class Capture:
sampling_times_minute = 1 # 每分钟采样次数
@classmethod
def capture(cls):
while True:
secs = sorted(cls.random_nums())
cur_time = time.localtime()
cls.logger.info("采集日志的时刻(秒):%s", str(secs))
while secs[-1] < cur_time.tm_sec:
time.sleep(0.4)
cur_time = time.localtime()
for sec in secs:
cur_time = time.localtime()
if sec < cur_time.tm_sec:
continue
while sec > cur_time.tm_sec:
time.sleep(0.4)
cur_time = time.localtime()
filename = cls.get_filename()
im = ImageGrab.grab()
im.save(filename)
cls.logger.info("生成文件:%s", filename)
@classmethod
def random_nums(cls):
return random.sample(range(0, 60), cls.sampling_times_minute)
US07-查看最新抓屏:
- Web服务对象:MonitorPageWebService
- new_capture_pic() 返回最新的图片
- main_run.new_capture_pic() URL映射
class MonitorPageWebService:
......
@classmethod
def new_capture_pic(cls):
filenames = os.listdir(cls.save_capture_pic_path)
filenames.sort(reverse=True)
new_filename = os.path.join(cls.save_capture_pic_path, filenames[0])
return send_file(new_filename, mimetype='image/png')
......
@classmethod
def main_run(cls):
......
@app.route('/new_capture_pic')
def new_capture_pic():
return cls.new_capture_pic()
......
US08-图片保存:
- 抓屏对象:Capture
- save_pic_path图片保存位置
- get_filename():在html代码中添加img的点击事件,获取新的图片,通过参数做到不从缓冲中获取图片
class Capture:
save_pic_path = os.path.join(os.path.dirname(__file__),
"static", "capture_pic")
logger = MyLogger.get("Capture", level=logging.DEBUG)
@classmethod
def capture(cls):
while True:
......
filename = cls.get_filename()
im = ImageGrab.grab()
im.save(filename)
cls.logger.info("生成文件:%s", filename)
@classmethod
def get_filename(cls):
now_str = time.strftime("%Y%m%d_%H%M%S")
filename = os.path.join(cls.save_pic_path, f"capture_{now_str}.png")
return filename
US09-点击刷新图片:
- Web服务对象:MonitorPageWebService
- get_html():在html代码中添加img的点击事件,获取新的图片,通过参数做到不从缓冲中获取图片
class MonitorPageWebService:
......
@classmethod
def get_html(cls, form_field):
html = """
......
<td>
<img id="displayPic" src="{{ cur_handler.imgSrc }}"
width="100%" height="100%" alt="monitor capture"
οnclick="img_display_pic_onclick(this)">
</td>
......
<script type="text/javascript">
function img_display_pic_onclick(elem){
var src = elem.src.split('?')[0];
src = src + '?' + (new Date().getTime());
elem.src = src;
}
</script>
</body>
</html>
"""
......