Python+QT5小工具开发:subprocess.Popen执行adb命令获取截图

2 篇文章 0 订阅

一:使用工具

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('截图保存路径:请连接手机')

三:运行效果

点击一键截图按钮,然后选择保存文件,亲测有效。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值