如何通过Python实现控制手机

发布时间:2021-10-08 11:28:34 作者:iii
来源:亿速云 阅读:202

这篇文章主要介绍“如何通过Python实现控制手机”,在日常操作中,相信很多人在如何通过Python实现控制手机问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何通过Python实现控制手机”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

安装

首先,转到此链接并在您的系统中下载 adb。
解压文件夹并将 adb 放入环境变量中。下面是在环境变量中添加 adb 的完整过程,

在您的手机中启用 USB 调试,并使用 USB 电缆将您的手机与 PC 连接。

开始

让我们首先导入一些我们需要的依赖项。您可以使用pip.

import cv2
import subprocess

我们将需要子进程通过命令行调用 adb 并获取输出,我们需要 cv2 进行一些图像处理,以便 python 能够点击屏幕或任何其他任务。
现在让我们在下面创建一个名为 adb 的基本函数,

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

上面的函数基本上是通过子进程调用 adb 并检索我们将需要的输出。

轻敲

现在让我们编写代码,其中 python 将单击移动设备的屏幕。所以我们将创建一个名为 tap 的函数,它会点击屏幕上的特定位置。

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

这将单击距 x 100 像素和距 y 100 像素。现在您一定在想,为每个命令硬编码坐标是非常困难的,并且当设备改变时它不会工作,这就是为什么在本博客的下一节中我们将使用图像处理来检测坐标自动地。

截图

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

代码很简单。我们制作了一个功能,可以保存手机内部图像目录的屏幕截图。在函数中,我们可以传递图像文件的名称。

高级点击

现在,我们将使用目标图像来自动检测坐标,而不是传递坐标。为了更好地理解这一点,让我们举个例子,我有这个屏幕 ,我想打开我们中间的应用程序,然后将使用一个称为. 通过这个过程,我们将截取屏幕截图 > 使用模板匹配计算我们中间图标的坐标 > 点击那里

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实现控制手机

我们还能做什么?

你可以用 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 的关键事件。如果您想知道,请访问此链接。

打开网址

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

发送 Whatsapp 消息

好的,所以我觉得这很酷。在获得了所有这些基本的理解之后,我们已经解决了我的主要问题,即发送没有二维码的 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')

到此,关于“如何通过Python实现控制手机”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 手机号EditText录入344控制
  2. 怎样通过python实现全排列

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:让web网页图片变灰色将彩色图像变成灰度的三种方法分别是怎样的

下一篇:Ajax提交表单数据到入库的全盘操作过程是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》