Loading... 需安装:jpegoptim ```sh #!/bin/bash # 预处理目录 pre_dir=/mnt/sda/cam/pre # 最终保存目录 dir=/mnt/sda/cam mkdir $pre_dir 2&>1 > /dev/null mkdir $dir 2&>1 > /dev/null # 【可选】上报电池信息,用于自动化充电 # adb shell dumpsys battery | grep -E 'level|AC' | # sed ':a;N;s/\n/,/g;ta' - | # sed 's/AC powered/AC/g' - | # sed 's/ //g' - | # sed 's/,/,"/g' - | # sed 's/:/":/g' - | # sed 's/^|&/"/g' - | # sed 's/^/{"/g' - | # sed 's/$/}/g' - | # xargs -I value -o mosquitto_pub -t 'phone/battery/level' -h 192.168.31.110 -p 9883 -m 'value' adb shell input keyevent 26 sleep 0.5 adb shell am start -a android.media.action.STILL_IMAGE_CAMERA sleep 0.5 adb shell input tap 539 900 sleep 1 adb shell input keyevent 27 sleep 1 adb shell input keyevent 4 adb shell input keyevent 26 adb shell find /sdcard/DCIM/Camera -name "*.jpg" | xargs -o -I path adb pull path $pre_dir adb shell rm /sdcard/DCIM/Camera/* # 【可选】压缩图片,修改size可控制图片大小 find $pre_dir -name "*.jpg" | xargs jpegoptim --size=500k mv $pre_dir/* $dir ``` <br/> python3版本: 添加了对屏幕是否亮屏的检测 ```python #!/bin/python3 import os import re import json import time adb_id = '192.168.31.129:5555' pre_dir = '/mnt/sda/cam/pre/' dir = '/mnt/sda/cam/' try: os.mkdir(dir) except: pass try: os.mkdir(pre_dir) except: pass def cmd(command, display=True): if display: print(command) out = os.popen(command).read() if display: print(out) return out def adb(shell, display=True): if not shell.startswith('adb'): shell = 'adb %s' % (shell) return cmd(shell.replace('adb', 'adb -s %s' % (adb_id)), display) def is_screenState(): return True if 'state=ON' in adb('shell dumpsys power | grep "Display Power: state="') else False def open_screen(): if not is_screenState(): adb('shell input keyevent 26') def close_screen(): if is_screenState(): adb('shell input keyevent 26') # 发送电量数据 def send_power_status(): info = adb('shell dumpsys battery', False) ac = True if re.compile( r'AC powered: ([^\n]+)').findall(info)[0] == 'true' else False level = int(re.compile(r'level: ([0-9]+)').findall(info)[0]) j = json.dumps({"level": level, "AC": ac}) os.popen( 'mosquitto_pub -t \'phone/oppo/battery\' -h 192.168.31.110 -p 9883 -m \'%s\'' % (j)) send_power_status() open_screen() adb('shell am start -a android.media.action.STILL_IMAGE_CAMERA') time.sleep(0.5) adb('shell input tap 539 900') time.sleep(1) adb('shell input keyevent 27') time.sleep(1) adb('shell input keyevent 4') close_screen() for path in adb('adb shell find /sdcard/DCIM/Camera -name "*.jpg"').split('\n'): if path == '': continue adb('adb pull %s %s' % (path, pre_dir)) adb('adb shell rm /sdcard/DCIM/Camera/*') for path in cmd('find %s -name "*.jpg"' % (pre_dir)).split('\n'): if path == '': continue cmd('jpegoptim --size=250k %s' % (path)) cmd('mv %s/* %s' % (pre_dir, dir)) ``` Last modification:June 30, 2023 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 如果觉得我的文章对你有用,请随意赞赏