欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過(guò)Python實(shí)現(xiàn)控制手機(jī)詳解

 更新時(shí)間:2021年10月08日 10:41:38   作者:初遇我ㄖ寸の熱情呢?  
如今python是非常多人學(xué)習(xí)的,而手機(jī)也幾乎人手一部。對(duì)于很多Python學(xué)習(xí)者,想用python來(lái)完成android手機(jī)中各種炫酷的的控制,adb是必不可缺少的工具之一

在這里插入圖片描述

幾天前我在考慮使用 python 從 whatsapp 發(fā)送消息。和你們一樣,我開(kāi)始潛伏在互聯(lián)網(wǎng)上尋找一些解決方案并找到了關(guān)于twilio. 一開(kāi)始,是一個(gè)不錯(cuò)的解決方案,但它不是免費(fèi)的,我必須購(gòu)買(mǎi)一個(gè) twilio 電話(huà)號(hào)碼。此外,我無(wú)法在互聯(lián)網(wǎng)上找到任何可用的 whatsapp API。所以我放棄了使用 twilio 和任何其他 whatsapp API 的想法。在想了很多之,打開(kāi) android studio,我連接了我的手機(jī),然后開(kāi)始了這個(gè)過(guò)程。當(dāng)應(yīng)用程序構(gòu)建時(shí),我想到了使用手機(jī)本身自動(dòng)發(fā)送 whatsapp 消息的想法。我搜索了一些差不多的東西,發(fā)現(xiàn)了一些很有可能解決我的問(wèn)題的東西。我找到了一個(gè)命令行工具,adb它可以幫助人們?cè)诓唤佑|手機(jī)的情況下控制手機(jī)。

你應(yīng)該擁有的東西

  • 對(duì) Python 的基本理解
  • 一些空閑時(shí)間閱讀此博客

安裝

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

在這里插入圖片描述

  • 在您的手機(jī)中啟用 USB 調(diào)試,并使用 USB 電纜將您的手機(jī)與 PC 連接。
  • 通過(guò)打開(kāi) cmd 并鍵入,檢查連接是否正確adb devices。您將在連接的設(shè)備列表中看到一個(gè)設(shè)備。
  • 如果您可以看到您的設(shè)備,那么您可以打開(kāi)任何代碼編輯器。我正在使用 Visual Studio 代碼。

開(kāi)始

讓我們首先導(dǎo)入一些我們需要的依賴(lài)項(xiàng)。您可以使用pip.

import cv2
import subprocess

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

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

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

輕敲

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

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

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

截圖

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

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

高級(jí)點(diǎn)擊

現(xiàn)在,我們將使用目標(biāo)圖像來(lái)自動(dòng)檢測(cè)坐標(biāo),而不是傳遞坐標(biāo)。為了更好地理解這一點(diǎn),讓我們舉個(gè)例子,我有這個(gè)屏幕 ,我想打開(kāi)我們中間的應(yīng)用程序,然后將使用一個(gè)稱(chēng)為. 通過(guò)這個(gè)過(guò)程,我們將截取屏幕截圖 > 使用模板匹配計(jì)算我們中間圖標(biāo)的坐標(biāo) > 點(diǎ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.

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

在這里插入圖片描述

我們還能做什么?

你可以用 adb 和 python 做更多的事情。讓我們談?wù)勂渲械囊恍?/p>

滑動(dòng)

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))

打電話(huà)給某人

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

從手機(jī)下載文件到電腦

在這里插入圖片描述

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

手機(jī)錄屏

# 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}")

打開(kāi)手機(jī)

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

還有更多類(lèi)似 26 的關(guān)鍵事件。如果您想知道,請(qǐng)?jiān)L問(wèn)此鏈接。

打開(kāi)網(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 消息

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

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')

消息已發(fā)送!

在這里插入圖片描述

到此這篇關(guān)于通過(guò)Python實(shí)現(xiàn)控制手機(jī)詳解的文章就介紹到這了,更多相關(guān)Python 控制手機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論