最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python編寫adb截圖工具的實(shí)現(xiàn)源碼

 更新時(shí)間:2021年08月20日 14:05:45   作者:mengyuelby  
adb截圖工具可用于Android手機(jī)及Android終端,Android端或者Android終端的遠(yuǎn)程截圖至本地電腦中,今天通過(guò)本文給大家介紹python編寫adb截圖工具的實(shí)現(xiàn)源碼,感興趣的朋友一起看看吧

一、 功能

Android端或者Android終端的遠(yuǎn)程截圖至本地電腦中

在這里插入圖片描述

二、使用說(shuō)明

1.adb截圖工具可用于Android手機(jī)及Android終端
2.使用數(shù)據(jù)線連接前提:電腦與Android終端/手機(jī)已連接
Android終端/手機(jī)已打開開發(fā)者模式
3.生成的圖片格式為png

三、實(shí)現(xiàn)

1.初始源碼

import time
import os,sys

#截圖
def screencap_cmd(filePath,devices=None):
    if filePath==None:
        filePath='D:/png/'
    if not os.path.exists(filePath):
        os.makedirs(filePath)
    filename=time.strftime("%Y%m%d%H%M%S",time.localtime())+".png"
    if devices==None:
        cmd_screencap='adb shell screencap -p sdcard/'+filename
        cmd_pull='adb pull sdcard/'+filename+' '+filePath+filename
        cmd_delete='adb shell rm sdcard/'+filename
    else:
        cmd_screencap='adb -s '+devices+' shell screencap -p sdcard/'+filename
        cmd_pull='adb -s '+devices+' pull sdcard/'+filename+' '+filePath+filename
        cmd_delete='adb -s '+devices+' shell rm sdcard/'+filename
    os.system(cmd_screencap)
    os.system(cmd_pull)
    os.system(cmd_delete)
#保存地址判斷及設(shè)備信息分類調(diào)用
def screencap_info(devices=None,filePath=None):
    if filePath==None:
        filePath='D:/png/'
    if not os.path.exists(filePath):
        os.makedirs(filePath)
    if devices==None:
        screencap_cmd(filePath)
        adb_device_none(filePath)
    else:
        screencap_cmd(filePath,devices)
        adb_info(devices,filePath)
#更換設(shè)備
def change_devices():
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    for i in lines:
        print(i.split("\t")[0])
    devices=input("請(qǐng)輸入指定設(shè)備:\n")
    for i in lines:
        if devices in i:
            return devices
    print("未找到改設(shè)備請(qǐng)重新輸入!")
    change_devices()
#截圖命令判斷,電腦連接多個(gè)設(shè)備或使用IP截圖時(shí)
def adb_info(lines,filePath=None):    
    s=input("是否截圖(T/F/N)?\n")
    if s=='t' or s=='T':
        if filePath==None:
            screencap_info(lines)
        else:
            screencap_info(lines,filePath)
    elif s=='N' or s=='n' or s=='exit':
        sys.exit()
    elif s=='F' or s=='f':
        devices=change_devices()
        if filePath==None:
            adb_info(devices)
        else:
            adb_info(devices,filePath)    
 #截圖命令判斷,電腦連接僅連接一個(gè)設(shè)備時(shí)  
def adb_device_none(filePath=None):
    s=input("是否截圖(T/F/N)?\n")
    if s=='t' or s=='T':
        if filePath==None:
            screencap_info()
        else:
            screencap_info(None,filePath)
    elif s=='N' or s=='n' or s=='exit':
        sys.exit()
    elif s=='F' or s=='f':
        devices=change_devices()
        if filePath==None:
            adb_info(devices)
        else:
            adb_info(devices,filePath)
#使用設(shè)備判斷
def adb_devices(n,lines,filePath=None):
    if n==0:
        print("請(qǐng)檢查是否已接連或啟動(dòng)調(diào)試模式或安裝adb環(huán)境")
        s=input("再次啟動(dòng)(T/F)?\n")
        if s=='t' or s=='T':
            r=os.popen("adb devices")
            lines=r.readlines()
            lines=lines[1:-1]
            n=len(lines)
            adb_devices(n,r.readlines())
        else:
            sys.exit()
    elif ":5555" in lines:
        print("T:截圖  F:更換設(shè)備 exit|N:退出 ")
        if filePath==None:
            adb_info(lines)
        else:
            adb_info(lines,filePath)   
    elif n==1:
        print("T:截圖  F:更換設(shè)備  exit|N:退出")
        if filePath==None:
            adb_device_none()
        else:
            adb_device_none(filePath)    
    elif n>1:
        for i in lines:
            print(i.split("\t")[0])
        devices=input("請(qǐng)輸入指定設(shè)備:\n")
        for i in lines:
            if devices in i:
                print("T:截圖  F:更換設(shè)備  exit|N:退出")
                if filePath==None:
                    adb_info(devices)
                else:
                    adb_info(devices,filePath)  
        print("未找到改設(shè)備請(qǐng)重新輸入!")
        if filePath==None:
            adb_devices(n,lines)
        else:  
            adb_devices(n,lines,filePath)

#輸入IP
def ip_info():
    ipPath=input("請(qǐng)輸入IP:(不輸入enter跳過(guò)默認(rèn)使用數(shù)據(jù)線連接)\n")
    if len(ipPath)>0:
        try:
            cnd='adb connect '+ipPath
            os.system(cnd)
            return ipPath
        except:
            print("ADB調(diào)試模式為USB,需要切換到無(wú)線調(diào)試模式\n")
            print("切換方法:\n第一步:Android設(shè)備開啟USB調(diào)試,并且通過(guò)USB線連接到電腦\n第二步:在終端執(zhí)行以下命令”adb tcpip 5555“\n第三步:在終端執(zhí)行以下命令”adb connect ip“。此時(shí)拔出USB線,應(yīng)該就可以adb通過(guò)wifi調(diào)試設(shè)備\n")
            return ip_info()
if  __name__ == '__main__':
    ipPath=ip_info()
    filePath=input("請(qǐng)輸入保存地址:(不輸入enter跳過(guò),默認(rèn)保存至D:\png\)\n")
    #查詢當(dāng)前連接的設(shè)備
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    ip_add=0
    if ipPath!=None:
        for i in lines:
            if ipPath in i:
                ip_add=i.split("\t")[0]
    if len(filePath)>0:
        if ":\\" in filePath:
            if ip_add!=0:
                adb_devices(n,ip_add,filePath)
            else:
                adb_devices(n,lines,filePath)
        else:
            print("地址輸入非法,使用默認(rèn)地址\n")
            if ip_add!=0:
                adb_devices(n,ip_add)
            else:
                adb_devices(n,lines)
    else:
        if ip_add!=0:
            adb_devices(n,ip_add) 
        else:
            adb_devices(n,lines) 

2.優(yōu)化:增加ip連接斷開重連處理

在輸入ip函數(shù)ip_info()增加ip連接判斷,將os.system替換成os.popen函數(shù)執(zhí)行cmd命令:

r=os.popen(cnd)
#讀取執(zhí)行結(jié)果
lines=r.readlines()[0]
if 'unable' in lines:
    print("連接失敗\n")
    return ip_info()
else:
    return ipPath
  1. os.system函數(shù)無(wú)法獲取執(zhí)行結(jié)果,只能獲取執(zhí)行成功或失敗的結(jié)果,成功返回0,失敗返回1,且會(huì)執(zhí)行的結(jié)果打印在終端上,相當(dāng)于調(diào)起dos執(zhí)行命令
  2. os.popen函數(shù)可獲取執(zhí)行的結(jié)果內(nèi)容,但獲取的結(jié)果要進(jìn)行readlines讀取,執(zhí)行的結(jié)果不會(huì)打印在終端上

在輸入ip函數(shù)ip_info()增加ip連接判斷,將os.system替換成os.popen函數(shù)執(zhí)行cmd命令:

a=os.system(cmd_screencap)
if a==0:
   os.system(cmd_pull)
   os.system(cmd_delete)
else:
   if ":5555" in devices:
            print("連接斷開,請(qǐng)重新連接\n")
            ipPath=ip_info()
            if ipPath==None:
                ipPath=ip_info()
            else:
                device=ipPath+":5555"
                adb_info(device,filePath)
        else:
            print("設(shè)備連接斷開,請(qǐng)更換設(shè)備\n")
            device=change_devices()
            adb_info(device,filePath)

3.最終源碼

import time
import os,sys

#輸入IP
def ip_info():
    ipPath=input("請(qǐng)輸入IP:(不輸入enter跳過(guò)默認(rèn)使用數(shù)據(jù)線連接)\n")
    if len(ipPath)>0:
        try:
            cnd='adb connect '+ipPath
            r=os.popen(cnd)
            lines=r.readlines()[0]
            if 'unable' in lines:
                print("連接失敗\n")
                return ip_info()
            else:
                return ipPath

        except:
            print("ADB調(diào)試模式為USB,需要切換到無(wú)線調(diào)試模式\n")
            print("切換方法:\n第一步:Android設(shè)備開啟USB調(diào)試,并且通過(guò)USB線連接到電腦\n第二步:在終端執(zhí)行以下命令”adb tcpip 5555“\n第三步:在終端執(zhí)行以下命令”adb connect ip“。此時(shí)拔出USB線,應(yīng)該就可以adb通過(guò)wifi調(diào)試設(shè)備\n")
            return ip_info()
#保存地址判斷及設(shè)備信息分類調(diào)用
def screencap_info(devices=None,filePath=None):
    if filePath==None:
        filePath='D:/png/'
    if not os.path.exists(filePath):
        os.makedirs(filePath)
    if devices==None:
        screencap_cmd(filePath)
        adb_device_none(filePath)
    else:
        screencap_cmd(filePath,devices)
        adb_info(devices,filePath)
#更換設(shè)備
def change_devices():
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    for i in lines:
        print(i.split("\t")[0])
    devices=input("請(qǐng)輸入指定設(shè)備:\n")
    for i in lines:
        if devices in i:
            return devices
    print("未找到改設(shè)備請(qǐng)重新輸入!\n")
    change_devices()
#截圖命令判斷,電腦連接多個(gè)設(shè)備或使用IP截圖時(shí)
def adb_info(lines,filePath=None):    
    s=input("是否截圖(T/F/N)?\n")
    if s=='t' or s=='T':
        if filePath==None:
            screencap_info(lines)
        else:
            screencap_info(lines,filePath)
    elif s=='N' or s=='n' or s=='exit':
        sys.exit()
    elif s=='F' or s=='f':
        devices=change_devices()
        if filePath==None:
            adb_info(devices)
        else:
            adb_info(devices,filePath)    
#截圖命令判斷,電腦連接僅連接一個(gè)設(shè)備時(shí)  
def adb_device_none(filePath=None):
    s=input("是否截圖(T/F/N)?\n")
    if s=='t' or s=='T':
        if filePath==None:
            screencap_info()
        else:
            screencap_info(None,filePath)
    elif s=='N' or s=='n' or s=='exit':
        sys.exit()
    elif s=='F' or s=='f':
        devices=change_devices()
        if filePath==None:
            adb_info(devices)
        else:
            adb_info(devices,filePath)
#使用設(shè)備判斷
def adb_devices(n,lines,filePath=None):
    if n==0:
        print("請(qǐng)檢查是否已接連或啟動(dòng)調(diào)試模式或安裝adb環(huán)境\n")
        s=input("再次啟動(dòng)(T/F)?\n")
        if s=='t' or s=='T':
            r=os.popen("adb devices")
            lines=r.readlines()
            lines=lines[1:-1]
            n=len(lines)
            adb_devices(n,r.readlines())
        else:
            sys.exit()
    elif ":5555" in lines:
        print("T:截圖  F:更換設(shè)備 exit|N:退出\n")
        if filePath==None:
            adb_info(lines)
        else:
            adb_info(lines,filePath)   
    elif n==1:
        print("T:截圖  F:更換設(shè)備  exit|N:退出\n")
        if filePath==None:
            adb_device_none()
        else:
            adb_device_none(filePath)    
    elif n>1:
        for i in lines:
            print(i.split("\t")[0])
        devices=input("請(qǐng)輸入指定設(shè)備:\n")
        for i in lines:
            if devices in i:
                print("T:截圖  F:更換設(shè)備  exit|N:退出")
                if filePath==None:
                    adb_info(devices)
                else:
                    adb_info(devices,filePath)  
        print("未找到改設(shè)備請(qǐng)重新輸入!")
        if filePath==None:
            adb_devices(n,lines)
        else:  
            adb_devices(n,lines,filePath)
#截圖
def screencap_cmd(filePath,devices=None):
    if filePath==None:
        filePath='D:/png/'
    if not os.path.exists(filePath):
        os.makedirs(filePath)
    filename=time.strftime("%Y%m%d%H%M%S",time.localtime())+".png"
    if devices==None:
        cmd_screencap='adb shell screencap -p sdcard/'+filename
        cmd_pull='adb pull sdcard/'+filename+' '+filePath+filename
        cmd_delete='adb shell rm sdcard/'+filename
    else:
        cmd_screencap='adb -s '+devices+' shell screencap -p sdcard/'+filename
        cmd_pull='adb -s '+devices+' pull sdcard/'+filename+' '+filePath+filename
        cmd_delete='adb -s '+devices+' shell rm sdcard/'+filename
    a=os.system(cmd_screencap)
    if a==0:
        os.system(cmd_pull)
        os.system(cmd_delete)
    else:
        if ":5555" in devices:
            print("連接斷開,請(qǐng)重新連接\n")
            ipPath=ip_info()
            if ipPath==None:
                ipPath=ip_info()
            else:
                device=ipPath+":5555"
                adb_info(device,filePath)
        else:
            print("設(shè)備連接斷開,請(qǐng)更換設(shè)備\n")
            device=change_devices()
            adb_info(device,filePath)
if  __name__ == '__main__': 
    ipPath=ip_info()
    filePath=input("請(qǐng)輸入保存地址:(不輸入enter跳過(guò),默認(rèn)保存至D:\png\)\n")
    #查詢當(dāng)前連接的設(shè)備
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    ip_add=0
    if ipPath!=None:
        for i in lines:
            if ipPath in i:
                ip_add=i.split("\t")[0]
    if len(filePath)>0:
        if ":\\" in filePath:
            if ip_add!=0:
                adb_devices(n,ip_add,filePath)
            else:
                adb_devices(n,lines,filePath)
        else:
            print("地址輸入非法,使用默認(rèn)地址\n")
            if ip_add!=0:
                adb_devices(n,ip_add)
            else:
                adb_devices(n,lines)
    else:
        if ip_add!=0:
            adb_devices(n,ip_add) 
        else:
            adb_devices(n,lines) 

到此這篇關(guān)于python編寫adb截圖工具的文章就介紹到這了,更多相關(guān)python adb截圖工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python連接Oracle之環(huán)境配置、實(shí)例代碼及報(bào)錯(cuò)解決方法詳解

    Python連接Oracle之環(huán)境配置、實(shí)例代碼及報(bào)錯(cuò)解決方法詳解

    這篇文章主要介紹了Python連接Oracle之環(huán)境配置、實(shí)例代碼及報(bào)錯(cuò)解決方法詳解,需要的朋友可以參考下
    2020-02-02
  • Python類的定義和使用詳情

    Python類的定義和使用詳情

    這篇文章主要介紹了Python類的定義和使用詳情,在Python中,類表示具有相同屬性和方法的對(duì)象的集合,文章圍繞主題相關(guān)資料展開更多的相關(guān)介紹,需要的小伙伴可以參考一下
    2022-06-06
  • python入門游戲之井字棋實(shí)例代碼

    python入門游戲之井字棋實(shí)例代碼

    python井字棋游戲雖然看上去非常簡(jiǎn)陋,但是卻非常值得學(xué)習(xí),下面這篇文章主要給大家介紹了關(guān)于python入門游戲之井字棋的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Python實(shí)現(xiàn)向好友發(fā)送微信消息

    Python實(shí)現(xiàn)向好友發(fā)送微信消息

    利用python可以實(shí)現(xiàn)微信消息發(fā)送功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過(guò)本文給大家分享使用?Python?實(shí)現(xiàn)微信消息發(fā)送的思路代碼,一起看看吧
    2022-06-06
  • django中forms組件的使用與注意

    django中forms組件的使用與注意

    這篇文章主要給大家介紹了關(guān)于django中forms組件的使用與注意的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python基于ID3思想的決策樹

    python基于ID3思想的決策樹

    這篇文章主要為大家詳細(xì)介紹了python基于ID3思想的決策樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python中用Spark模塊的使用教程

    Python中用Spark模塊的使用教程

    這篇文章主要介紹了Python中用Spark模塊的使用教程,來(lái)自IBM官方技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • 淺談Python的Django框架中的緩存控制

    淺談Python的Django框架中的緩存控制

    這篇文章主要介紹了Python的Django框架中的緩存控制,包括Vary頭部以外的其他頭部使用等,需要的朋友可以參考下
    2015-07-07
  • Python中字典的setdefault()方法教程

    Python中字典的setdefault()方法教程

    在學(xué)習(xí)python字典操作方法時(shí),感覺(jué)setdefault()方法,比字典的其它基本操作方法更難理解的同學(xué)比較多,所以想著總結(jié)以下,下面這篇文章主要給大家介紹了Python中字典的setdefault()方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02
  • python 構(gòu)造三維全零數(shù)組的方法

    python 構(gòu)造三維全零數(shù)組的方法

    今天小編就為大家分享一篇python 構(gòu)造三維全零數(shù)組的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11

最新評(píng)論

来安县| 昌都县| 正宁县| 鄂州市| 化德县| 玉田县| 东城区| 黄浦区| 中超| 新巴尔虎左旗| 永济市| 桃园市| 南江县| 韩城市| 江口县| 宁南县| 宜黄县| 轮台县| 望江县| 玉田县| 信宜市| 霍林郭勒市| 遵化市| 湘潭县| 手游| 静宁县| 晴隆县| 肇东市| 海丰县| 邯郸县| 霍邱县| 永清县| 马龙县| 石柱| 忻城县| 淅川县| 安塞县| 西藏| 虎林市| 丰顺县| 祥云县|