溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何通過Python實現(xiàn)控制手機

發(fā)布時間:2021-10-08 11:28:34 來源:億速云 閱讀:215 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“如何通過Python實現(xiàn)控制手機”,在日常操作中,相信很多人在如何通過Python實現(xiàn)控制手機問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何通過Python實現(xiàn)控制手機”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

安裝

首先,轉(zhuǎn)到此鏈接并在您的系統(tǒng)中下載 adb。
解壓文件夾并將 adb 放入環(huán)境變量中。下面是在環(huán)境變量中添加 adb 的完整過程,

在您的手機中啟用 USB 調(diào)試,并使用 USB 電纜將您的手機與 PC 連接。

  • 通過打開 cmd 并鍵入,檢查連接是否正確adb devices。您將在連接的設備列表中看到一個設備。

  • 如果您可以看到您的設備,那么您可以打開任何代碼編輯器。我正在使用 Visual Studio 代碼。

開始

讓我們首先導入一些我們需要的依賴項。您可以使用pip.

import cv2
import subprocess

我們將需要子進程通過命令行調(diào)用 adb 并獲取輸出,我們需要 cv2 進行一些圖像處理,以便 python 能夠點擊屏幕或任何其他任務。
現(xiàn)在讓我們在下面創(chuàng)建一個名為 adb 的基本函數(shù),

def adb(command):
    proc = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, shell=True)
    (out, _) = proc.communicate()
    return out.decode('utf-8')

上面的函數(shù)基本上是通過子進程調(diào)用 adb 并檢索我們將需要的輸出。

輕敲

現(xiàn)在讓我們編寫代碼,其中 python 將單擊移動設備的屏幕。所以我們將創(chuàng)建一個名為 tap 的函數(shù),它會點擊屏幕上的特定位置。

def tap(tap_x, tap_y):
    adb("adb shell input tap {} {}".format(tap_x, tap_y))
tap(100,100)

這將單擊距 x 100 像素和距 y 100 像素?,F(xiàn)在您一定在想,為每個命令硬編碼坐標是非常困難的,并且當設備改變時它不會工作,這就是為什么在本博客的下一節(jié)中我們將使用圖像處理來檢測坐標自動地。

截圖

def take_screenshot(final):
    adb(f"adb exec-out screencap -p > ./images/{final}.png")

代碼很簡單。我們制作了一個功能,可以保存手機內(nèi)部圖像目錄的屏幕截圖。在函數(shù)中,我們可以傳遞圖像文件的名稱。

高級點擊

現(xiàn)在,我們將使用目標圖像來自動檢測坐標,而不是傳遞坐標。為了更好地理解這一點,讓我們舉個例子,我有這個屏幕 ,我想打開我們中間的應用程序,然后將使用一個稱為. 通過這個過程,我們將截取屏幕截圖 > 使用模板匹配計算我們中間圖標的坐標 > 點擊那里

TemplateMatching

ef image_position(small_image, big_image):
    img_rgb = cv2.imread(big_image)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(small_image, 0)
    height, width = template.shape[::]
    res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
    _, _, top_left, _ = cv2.minMaxLoc(res)
    bottom_right = (top_left[0] + width, top_left[1] + height)
    return (top_left[0]+bottom_right[0])//2, (top_left[1]+bottom_right[1])//2

screen="screen"
take_screenshot(screen)
x, y  = image_position("images/among_us_icon.png", f"images/{screen}")
click(x,y)
# WOWWW Python successfully opened among us app.

有了上面的代碼,即使你在手機屏幕上改變了我們游戲的位置,python仍然可以打開游戲。

如何通過Python實現(xiàn)控制手機

我們還能做什么?

你可以用 adb 和 python 做更多的事情。讓我們談談其中的一些。

滑動

def swipe(start_x, start_y, end_x, end_y, duration_ms):
    adb("adb shell input swipe {} {} {} {} {}".format(start_x, start_y, end_x, end_y, duration_ms))

打電話給某人

def call(number):
    adb(f"adb shell am start -a android.intent.action.CALL -d tel:{number}")
call('+91xxxxxxxxxx') # +[CODE][NUMBER]

從手機下載文件到電腦

在這里插入圖片描述

def download(path, output_path):
    adb(f"adb pull {path} {output_path}")
從手機中刪除文件
def remove(path):
    adb(f"adb shell rm {path}") #/sdcard/...

手機錄屏

# name is the video_file name and time is the seconds you want to record
def screen_record(name, time):
    adb(f"adb shell screenrecord /sdcard/{name} --time-limit {time}")
    download(f"/sdcard/{name}",f"./mobile/{name}")
    remove(f"/sdcard/{name}")

打開手機

def switch_phone_on_off():
    adb("adb shell input keyevent 26")

還有更多類似 26 的關(guān)鍵事件。如果您想知道,請訪問此鏈接。

打開網(wǎng)址

def open_url(url):
    adb(f'adb shell am start -a android.intent.action.VIEW -d {url}')
open_url("https://www.google.co.in/")

發(fā)送 Whatsapp 消息

好的,所以我覺得這很酷。在獲得了所有這些基本的理解之后,我們已經(jīng)解決了我的主要問題,即發(fā)送沒有二維碼的 whatsapp 消息,沒有像 twilio 這樣的付費方法。這有點棘手,但它在我的手機上工作。我希望它也適用于你的。

def send_whatsapp_message(phone, message):
    adb(f'adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone={phone}"') # Opening whatsapp url
    adb('ping 127.0.0.1 -n 2 > nul') # delay
    adb(f'adb shell input text "{message}"')  # entering message
    adb('adb shell keyevent 22') # Right arrow
    adb('adb shell keyevent 22') # Right arrow
    adb('adb shell input keyevent 22') # Right arrow 
    adb('adb shell input keyevent 22') # Right arrow
    adb('adb shell input keyevent 66') # Enter Key

send_whatsapp_message('+91xxxxxxxxxx', 'blah blah blah')

到此,關(guān)于“如何通過Python實現(xiàn)控制手機”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI