安卓自动化之minicap截图

安卓自动化之minicap截图

关于安卓自动化使用找图方法点击时,最大的痛点是使用原生adb截图速度太慢了,大概需要3s的时间,再加上我们使用opencv的找图算法,时间就去都三秒多了,为了解决这一个痛点,我们就可以使用minicap,他是通过在安卓常驻一个miniservice,通过adb跟我们的程序进行通信,它实时获取屏幕信息转化为数据流发送给我们的程序,我们只需实时去接收数据并把它转化成图片即可
1.第一步当然是先在安卓上安装我们的miniservice

python 复制代码
pip install uiautomator2 
python -m uiautomator2 init 
or
import uiautomator2 as u2
device =u2.connect()

我们在初始化的时候或者使用uiautomator2时会默认安装适合我们安卓架构的minicapservice
2.第二部使用adb命令启动安卓中的minicapservice

python 复制代码
adb forward tcp:1717 localabstract:minicap
adb shell LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P 6480x960@6480x960/0

6480x960@6480x960 是我们安卓设备的屏幕像素

python 复制代码
adb shell wn size

3.第三步,编写接收数据,并转换成图片

python 复制代码
import socket
import sys
import struct
from collections import OrderedDict


class Banner:
    def __init__(self):
        self.__banner = OrderedDict(
            [('version', 0),
             ('length', 0),
             ('pid', 0),
             ('realWidth', 0),
             ('realHeight', 0),
             ('virtualWidth', 0),
             ('virtualHeight', 0),
             ('orientation', 0),
             ('quirks', 0)
             ])

    def __setitem__(self, key, value):
        self.__banner[key] = value

    def __getitem__(self, key):
        return self.__banner[key]

    def keys(self):
        return self.__banner.keys()

    def __str__(self):
        return str(self.__banner)


class MiniCap:
    def __init__(self, host, port, banner):
        self.buffer_size = 4096
        self.host = host
        self.port = port
        self.banner = banner
        self.socket = None
        self.stop_flag = False

    def connect(self):
        """
        Connect to the specified host and port.

        Raises:
            socket.error: If there is any error creating the socket or connecting.
        """
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.socket.connect((self.host, self.port))
        except socket.error as e:
            print(f"Failed to connect to {self.host}:{self.port}. Error: {e}")
            raise e
    @classmethod
    def save_image(cls, data):
        file_name = 'received_image.jpg'  # 图片名
        with open(file_name, 'wb') as f:
            for b in data:
                f.write(b.to_bytes(1, 'big'))

    def consume(self,stop_flag):
        readBannerBytes = 0
        bannerLength = 24
        readFrameBytes = 0
        frameBodyLength = 0
        data = []
        while not stop_flag:
            try:
                chunk = self.socket.recv(self.buffer_size)
            except socket.error as e:
                print(e)
                sys.exit(1)
            cursor = 0
            buf_len = len(chunk)
            while cursor < buf_len:
                if readBannerBytes < bannerLength:
                    map(lambda i, val: self.banner.__setitem__(self.banner.keys()[i], val),
                        [i for i in range(len(self.banner.keys()))], struct.unpack("<2b5ibB", chunk))
                    cursor = buf_len
                    readBannerBytes = bannerLength
                elif readFrameBytes < 4:
                    frameBodyLength += (chunk[cursor] << (readFrameBytes * 8)) >> 0
                    cursor += 1
                    readFrameBytes += 1
                else:
                    if buf_len - cursor >= frameBodyLength:
                        data.extend(chunk[cursor:cursor + frameBodyLength])
                        self.save_image(data)
                        cursor += frameBodyLength
                        frameBodyLength = readFrameBytes = 0
                        data = []
                    else:
                        data.extend(chunk[cursor:buf_len])
                        frameBodyLength -= buf_len - cursor
                        readFrameBytes += buf_len - cursor
                        cursor = buf_len

    def run(self,stop_flag):
        """
        Connect to the specified host and port, then consume all frames from the socket.
        Raises:
            socket.error: If there is any error creating the socket or connecting.
        """
        try:
            self.connect()
            self.consume(stop_flag)
        except socket.error as e:
            print(f"Failed to connect to {self.host}:{self.port}. Error: {e}")
            raise e
 if '__main__' == __name__:
    # time.sleep(1)
    mc = MiniCap('localhost', 1717, Banner())
    mc.run(True)

当我们运行程序的时候就会在当前目录生成一个图片received_image.jpg,这个时候我们就完成了截图啦,但是这个图片实时更新的我们的其他程序是没有办法去读取图片的,这个时候就需要我们去截图啦,什么还要去截图,太麻烦了吧,不,准确的来说只是copy一下啦,截图就是copy一下,谁能有我截图快,快如电,嗖嗖嗖的

python 复制代码
  def mini_cap_screen(self, local_path, time_out):
        """
        mini cap截图
        Args:
            local_path: 目标路径
            time_out: 超时时间(秒)
        Returns:
            local_path 或 None
        """
        src_path = "received_image.jpg"
        end_time = datetime.now() + timedelta(seconds=time_out)
        start_time = datetime.now()
        self.logger.info(f"mini cap 截图开始时间: {start_time}")

        while datetime.now() < end_time:
            if os.path.exists(src_path) and os.path.getsize(src_path) > 200000:
                os.makedirs(os.path.dirname(local_path), exist_ok=True)
                shutil.copy2(src_path, local_path)
                self.logger.info(f"Image copied to {local_path}")

                current_time = datetime.now()
                self.logger.info(f"mini cap 截图结束时间: {current_time}")
                self.logger.info(f"mini cap 截图耗时: {current_time - start_time}")

                return local_path
            time.sleep(0.1)

        self.logger.error("mini cap 截图失败,请检测mini cap service is running")
        return None

本人天下第一快,NO,我说的是程序,经过计算我们截图加opencv找图然后在模拟点击只需250毫秒,嘎嘎快

相关推荐
Bigcrab__5 分钟前
Python3网络爬虫开发实战(15)Scrapy 框架的使用(第一版)
爬虫·python·scrapy
晨春计23 分钟前
【git】
android·linux·git
易辰君1 小时前
Python编程 - 协程
开发语言·python
宇宙第一小趴菜1 小时前
探索网络世界:TCP/IP协议、Python Socket编程及日常生活比喻
网络·python·tcp/ip
小黑031 小时前
Phoenix使用
开发语言·python
南斯拉夫的铁托1 小时前
(PySpark)RDD实验实战——求商品销量排行
python·spark·pyspark
标标大人1 小时前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言
kay_5451 小时前
YOLOv8改进 | 模块缝合 | C2f 融合SCConv提升检测性能【CVPR2023】
人工智能·python·深度学习·yolo·目标检测·面试·yolov8改进
kngines1 小时前
【PLW004】基于Python网络爬虫与推荐算法的新闻推荐平台v1.0(Python+Django+NLP+Vue+MySQL前后端分离)
爬虫·python·nlp·推荐算法
激动的兔子2 小时前
Python_两个jpg图片文件名称互换
python