原創 電腦控制手機 Python語言實現顏色識別功能示例
電腦能控制手機嗎?可以同時控制多部手機嗎?當然是可以的。通過手機多控軟件就可以實現這個功能。Total Control作爲安卓手機的多控系統,是我用過的各方面都很穩定不僅可以實現同時控制多到100部手機,還可以通過腳本實現各種功能的軟件。識別顏色是其強大功能之一,本文我就說說如何用Total Control 提供的REST API 實現單點比較顏色,支持顏色範圍,相似度比較顏色。
Total Control 提供的REST API:
請求參數:
請求示例:
http://localhost:8090/TotalControl/v1/devices/device@1116106541/screen/colors/color?token=270eq7lXQK8bXYsJ&x=566&y=729&color=0xffffff&sim=0.3
響應示例:
{
"value":"",
"status":true
}
Python示例:
#!/user/bin/python
#-*- coding:utf-8 -*-
import
import requests
# The username and password are separated by a single : and sent on the wire encoded
user_pass = 'sigma:3D391497'
encodeStr = .b64encode(user_pass.encode("UTF-8"))
# First step: Get the API token
LoginUrl = "http://localhost:8090/TotalControl/v1/login"
response = requests.get(LoginUrl, headers={'Authorization':encodeStr})
print("Get the token,Return value: ",response.json())
token = response.json()['value']['token']
print("The value of token is: ",token)
# Second step: Get the device id value of the master device
if token is not None:
GetDeviceUrl = "http://localhost:8090/TotalControl/v1/devices/main?token=" + token
response = requests.get(url=GetDeviceUrl)
print("Get the device id,Return value: ", response.json())
device = response.json()['id']
print("The value of device id is: ", device)
if device is not None:
# Third step: Execute this REST API
APIUrl = "http://localhost:8090/TotalControl/v1/devices/" + device + "/screen/colors/color"
data = {
"x": 566,
"y": 729,
"color": "0xffffff",
"sim": 0.9,
"token": token
}
response = requests.get(url=APIUrl, params=data)
ret = response.json()['status']
# Determine if this REST API is executed successfully
if ret is True:
print("Executed successfully,Return value: ", response.json())
elif response.status_code == 200 and ret is not True:
print("An error message is returned: ",response.json())
else:
print("This API failed to execute.")
else:
print("Failed to get device id.")
else:
print("Failed to get token.")
運行結果:
Get the token,Return value: {'status': True, 'value': {'token': 'lo7ssQ12KgkM6ik7'}}
The value of token is: lo7ssQ12KgkM6ik7
Get the device id,Return value: {'id': 'device@33254183'}
The value of device id is: device@33254183
Executed successfully,Return value: {'status': True, 'value': ''}
看起來複雜,其實確實有點複雜,不過別怕,Total Control就是考慮到很多腳本語言並不是特別容易掌握,所以才提供了Rest API,我們可以選擇簡單的腳本語言入手。Total Control會幫我做轉換,速度官網下載走起!