In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to control the mobile phone through Python". In the daily operation, I believe that many people have doubts about how to control the mobile phone through Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to control the mobile phone through Python". Next, please follow the editor to study!
Installation
First, go to this link and download adb on your system.
Extract the folder and put adb in the environment variable. Here is the complete process of adding adb to the environment variable
Enable USB debugging in your phone and connect your phone to PC using a USB cable.
Check that the connection is correct adb devices by opening cmd and typing. You will see a device in the list of connected devices.
If you can see your device, you can open any code editor. I am using Visual Studio code.
Start
Let's first import some of the dependencies we need. You can use pip.
Import cv2import subprocess
We will need the child process to call adb from the command line and get the output, and we will need cv2 to do some image processing so that python can click on the screen or any other task.
Now let's create a basic function called adb below
Def adb (command): proc = subprocess.Popen (command.split (''), stdout=subprocess.PIPE, shell=True) (out, _) = proc.communicate () return out.decode ('utf-8')
The above function basically calls adb through the child process and retrieves the output we will need.
Tap
Now let's write code where python will click on the screen of the mobile device. So we're going to create a function called tap that clicks on a specific location on the screen.
Def tap (tap_x, tap_y): adb ("adb shell input tap {} {}" .format (tap_x, tap_y)) tap (100100)
This will click the distance x 100 pixels and the distance y 100 pixels. Now you must be thinking that it is very difficult to hard-code coordinates for each command, and it will not work when the device changes, which is why we will use image processing to detect coordinates automatically in the next section of this blog.
Screenshot def take_screenshot (final): adb (f "adb exec-out screencap-p >. / images/ {final} .png")
The code is simple. We have created a feature that can save screenshots of the phone's internal image directory. In the function, we can pass the name of the image file.
Advanced click
Now, we will use the target image to automatically detect coordinates instead of passing them. To better understand this, let's give an example. I have this screen, I want to open an application among us, and then I will use a program called. Through this process, we will take a screenshot > use template matching to calculate the coordinates of our middle icons > click there
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]) / / 2screen = "screen" take_screenshot (screen) x image_position y = image_position ("images/among_us_icon.png", f "images/ {screen}") click (x Magney) # WOWWW Python successfully opened among us app.
With the above code, even if you change the position of our game on the phone screen, python can still open the game.
What else can we do?
You can do more with adb and python. Let's talk about some of them.
Slide 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)) call someone def call (number): adb (f "adb shell am start-an android.intent.action.CALL-d tel: {number}") call ('+ 91xxxxxxxxxxxx') # + [CODE] [NUMBER] download files from the phone to the computer
Insert a picture description here
Def download (path, output_path): adb (f "adb pull {path} {output_path}") delete the file def remove (path): adb (f "adb shell rm {path}") # / sdcard/... from the phone Name is the video_file name and time is the seconds you want to recorddef 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}") Open the phone def switch_phone_on_off (): adb ("adb shell input keyevent 26")
There are more key events like 26. If you want to know, please visit this link.
Open the URL def open_url (url): adb (f'adb shell am start-an android.intent.action.VIEW-d {url}') open_url ("https://www.google.co.in/") sends Whatsapp message"
Okay, so I think it's cool. With all this basic understanding, we have solved my main problem of sending whatsapp messages without a QR code, and there is no payment method like twilio. It's a little tricky, but it works on my phone. I hope it applies to you, too.
Def send_whatsapp_message (phone Message): adb (f'adb shell am start-an android.intent.action.VIEW-d "https://api.whatsapp.com/send?phone={phone}"') # Opening whatsapp url adb ('ping 127.0.0.1-N2 > 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 Keysend_whatsapp_message (' + 91xxxxxxxxxxxx' 'blah blah blah') so far The study on "how to control the mobile phone through Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.