【教程】手机远程连接另一台手机的termux进行拍照
生活随笔
收集整理的這篇文章主要介紹了
【教程】手机远程连接另一台手机的termux进行拍照
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 主要通過MQTT實現通信。圖片的上傳和顯示使用了服務器。
? ? 對于沒有服務器的同學,可以進入官網領取免費1個月的輕量云服務器:云產品免費試用;需要選購的進:輕量應用服務器專場;不清楚怎么操作的可以看教程:騰訊云產品免費試用教程
實現效果:
? ? ? ? 當然這只是一個示例,只要能實現手機間的通信,那就可以借助termux-api實現非常多的功能!
被控端手機(termux)
termux安裝mqtt等庫:
pip install paho-mqtt, requests, imagemagick在termux中的Python代碼為:
# coding:utf-8 import paho.mqtt.client as pahomqtt import os import base64 import requests import timeclass MQTT:def __init__(self):self.CLIENTID = 'redmi8'self.MQTTHOST = "test.mosquitto.org"self.MQTTPORT = 1883self.USERNAME = 'lh'self.PASSWORD = "hualala"self.HEARTBEAT = 60self.topic_subscribe = r'/mqtt/redmi8'self.topic_publish = r'/mqtt/redmi8/send/msg'self.topic_publish_image = r'/mqtt/redmi8/send/image'self.client = pahomqtt.Client()self.CloudAllowRun = 1# MQTT連接回調def on_connect(self, client, userdata, flags, rc):if rc == 0:print("Connected")client.subscribe(self.topic_subscribe)# MQTT接收回調def on_message(self, client, userdata, message):result = 'ok'msg = str(message.payload.decode("utf-8"))print("Received message: " + msg)if msg.startswith('cmd:img'):_, cmd, path = msg.split(' ')print('path:', path)if cmd == 'upload':img = self.upload_image(path)if img:self.client.publish(self.topic_publish_image, img)result = 'upload ok'else:result = 'upload fail'elif cmd == 'remove':self.remove_image(path)result = 'remove ok'else:# 執行本地cmd命令,并將結果保存到變量result中result = os.popen(msg).read().strip() or 'capture ok'time.sleep(1)if self.upload_image(path=msg.split()[-1]):result += '\nupload ok'print("Command result: " + result)self.client.publish(self.topic_publish, result)def remove_image(self, path="a.png"):url = 'http://xfxuezhang.cn/web/picture/remove_file.php?name='+pathif os.path.exists(path):os.remove(path)return requests.get(url).textdef upload_image(self, path="a.png"):result = Nonetry:url = 'http://xfxuezhang.cn/web/picture/upload_file.php'os.popen(f'convert {path} -resize 200x150 {path}')filename = path.split('/')[-1]files = {"file": (filename, open(path, "rb"), "image/png")}response = requests.post(url, files=files)print(response.text)result = response.json()['data']except Exception as e:print(e)return resultdef convert_image(self, path="a.png"):os.popen(f'convert {path} -resize 200x150 {path}')# 從文件中讀取圖片并將其轉換為二進制數據with open(path, "rb") as f:image_bytes = f.read()# 將二進制數據編碼為base64字符串image_base64 = base64.b64encode(image_bytes).decode("utf-8")return image_base64def mqtt(self):self.client.on_connect = self.on_connectself.client.on_message = self.on_messageself.client.username_pw_set(self.USERNAME, self.PASSWORD)try:self.client.connect(self.MQTTHOST, self.MQTTPORT, self.HEARTBEAT)# self.client.loop_start() # 線程self.client.loop_forever()except:passdef publish(self, msg):self.client.publish(topic=self.topic_publish, payload=msg, qos=1)if __name__ == '__main__':Mqtt = MQTT()Mqtt.mqtt()# while True:# time.sleep(10)# msg = input('>> ')# Mqtt.publish(msg)運行記錄:
控制端手機
另一臺控制端的手機上安裝“MQTT Dashboard”,并添加一些組件,其中的主題根據上面的代碼來填。實現效果:
總結
以上是生活随笔為你收集整理的【教程】手机远程连接另一台手机的termux进行拍照的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将Unity场景以Wavefront O
- 下一篇: SVG缩放平移优化