如何用Python控制自己的手机!再也不用担心开车接电话违章了

前言

       大家好,几天前,我正在考虑使用python从WhatsApp发送消息。就像你们一样,我开始潜伏在互联网上寻找一些解决方案吐里奥。在一开始,这似乎是一个很好的解决方案,但它不是免费的,我不得不购买一个Twilio电话号码。而且,我无法在互联网上找到任何WhatsAppAPI。因此,我放弃了使用Twilio和任何其他WhatsAppAPI的想法。想了很多之后,我累了,打开了Android工作室,开始开发我的一个应用程序。我接通了我的电话,然后开始了这个过程。当这个应用程序构建的时候,我想到了一个想法,那就是用手机本身来实现WhatsApp消息传递的自动化。我搜索了一下同样的东西,发现了一些有很大潜力解决我的问题的东西。我找到了一个命令行工具adb这有助于一个人控制电话而不碰手机。

你应该拥有的东西

  • 对Python的基本理解
  • 一些空闲时间来阅读这个博客

安装

  • 首先,来看看这个链接然后在你的系统里下载亚行。
  • 提取文件夹并将亚行放入环境变量中。下面是在环境变量中添加亚行的完整过程,

    如何用Python控制自己的手机!再也不用担心开车接电话违章了_第1张图片

    如何用Python控制自己的手机!再也不用担心开车接电话违章了_第2张图片
  • 在您的移动电话中启用USB调试,并使用USB电缆将您的手机与PC连接。
  • 通过打开cmd并键入adb devices。您将在附加设备列表中看到设备。
  • 如果您可以看到您的设备,那么您可以打开任何代码编辑器。我使用的是可视化演播室代码。

我们开始吧

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

import cv2
import subprocess

我们将需要子进程通过命令行调用亚行并获得输出,我们需要CV2进行一些图像处理,以便python能够单击屏幕或任何其他任务。

现在让我们在下面创建一个名为亚行的基本函数,

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

基本上,上面的功能是通过子进程调用亚行,并检索我们需要的输出。

水龙头

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

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

这将从x单击100 px,从y单击100 px。现在您一定认为很难对每个命令的坐标进行硬编码,当设备被更改时,它将无法工作,这就是为什么在这个博客的下一部分中,我们将使用图像处理来自动检测坐标。

截图

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

代码很简单。我们已经做了一个功能,保存屏幕截图内的移动图像目录。在函数中,我们可以传递图像文件的名称。

高级TAP

现在,我们将使用目标图像来自动检测坐标,而不是传递坐标。为了更好地理解这一点,让我们举个例子,我有这个屏幕

如何用Python控制自己的手机!再也不用担心开车接电话违章了_第3张图片

如何用Python控制自己的手机!再也不用担心开车接电话违章了_第4张图片

我想打开我们之间的应用程序,然后使用一个名为TemplateMatching。在这个过程中,我们将获取屏幕的屏幕截图>使用模板匹配计算我们之间图标的坐标>单击那里

# Here small_image is among us icon and big_image is the complete screenshot containing the among us icon
def 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,还有更多的事情可以做。让我们谈谈他们中的一些。

滑动

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]

从Mobile下载文件到PC

def download(path, output_path):
    adb(f"adb pull {path} {output_path}")

从Mobile中删除文件

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。如果你想知道,那就去看看这链接。

打开URL

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消息时没有QR代码,也没有像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,程序员,python,android)