一:使用工具
Python3.7+QT5
二:直接上代码,因为我是无私奉献的人。
#手机截图
def screencap(self):
file_path = QFileDialog.getSaveFileName(self, "保存截图", "./","png files (*.png);;jpg files (*.jpg);;all files(*.*)")
#截图名字
screencap_name = os.path.split(file_path[0])[1]
#截图目录
screencap_path = os.path.split(file_path[0])[0]
screencapCmd1 = 'adb shell screencap -p /sdcard/'+ screencap_name
screencapCmd2 = 'adb pull /sdcard/' +screencap_name+' '+screencap_path
if len(str(file_path[0])) > 0:
# 启动线程
threading.Thread(target=self.screencap_handle, args=(screencapCmd1,screencapCmd2,file_path[0],)).start()
else:
self.screencap_path.setText('截图保存路径:请选择路径')
def screencap_handle(self,screencapCmd1,screencapCmd2,file_path):
#判断是否连接手机
list2 = ''
ps = subprocess.Popen(self.deviceNameCmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=False)
with ps:
for line in ps.stdout:
list2 = list2+(line.decode("utf-8"))
adb_result = list2.replace('\n','').replace('\r','')
if(len(adb_result.split()))>4:
self.screencap_path.setText('截图保存路径:' + str(file_path))
subprocess.Popen(screencapCmd1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=False)
sleep(1)
subprocess.Popen(screencapCmd2, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=False)
else:
self.screencap_path.setText('截图保存路径:请连接手机')
三:运行效果
点击一键截图按钮,然后选择保存文件,亲测有效。