You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

能否通过adb或其他方式高效获取屏幕16x16局部区域?

Efficiently Capture a 16x16 Android Screen Region with Python & ADB

Absolutely! You don’t need to be an Android developer to grab just that tiny 16x16 screen region efficiently—no more wasting time pulling full screenshots and cropping them locally. Here are a couple of practical, Python-friendly methods using ADB that fit your workflow:

Method 1: ADB + Raw Bitmap Screenshot (No Extra Device Tools Needed)

This approach leverages ADB's built-in screencap tool to capture raw pixel data, then uses dd to extract only the bytes corresponding to your target region. It works on most stock Android devices without requiring additional apps.

Step-by-Step Python Implementation

import subprocess
from PIL import Image

def get_raw_screen_region(x, y, width=16, height=16):
    # Fetch device screen dimensions
    size_result = subprocess.check_output(
        ["adb", "shell", "wm", "size"]
    ).decode().strip()
    screen_width = int(size_result.split("x")[0].split()[-1])
    screen_height = int(size_result.split("x")[1])
    
    # Most modern devices use RGBA (4 bytes per pixel); adjust to 2 for RGB565 if needed
    bytes_per_pixel = 4
    
    # Calculate byte offsets for the target region
    start_byte = (y * screen_width + x) * bytes_per_pixel
    total_bytes_needed = width * height * bytes_per_pixel
    
    # Use ADB exec-out to run screencap and dd directly (avoids saving to device storage)
    capture_cmd = [
        "adb", "exec-out",
        f"screencap -b | dd skip={start_byte // 512} count={total_bytes_needed // 512 + 1} bs=512"
    ]
    
    # Pull the raw pixel data
    raw_data = subprocess.check_output(capture_cmd)
    # Trim to the exact region (dd may return extra bytes from the final block)
    region_bytes = raw_data[start_byte % 512 : start_byte % 512 + total_bytes_needed]
    
    # Convert bytes to a usable image
    return Image.frombytes("RGBA", (width, height), region_bytes)

# Example: Grab a 16x16 region starting at (100, 200)
small_region = get_raw_screen_region(100, 200)
small_region.save("tiny_screen_region.png")

Key Notes

  • If the output image looks distorted or garbled, your device might use a 2-byte pixel format (RGB565). Change bytes_per_pixel to 2 and update the Image.frombytes mode to "RGB" or "BGR" depending on your device's endianness.
  • The dd command uses a block size of 512 to align with ADB's transfer buffer, which speeds up data retrieval compared to reading byte-by-byte.

Method 2: Minicap (Ultra-Efficient Region Capture)

For even better performance—especially if you need to capture small regions frequently—use Minicap, a lightweight tool designed for fast, partial screen capture. It’s optimized to pull only the exact pixels you need without processing the entire screen.

Setup Steps

  1. Download the Minicap binary and corresponding minicap.so library that matches your device's CPU architecture (e.g., arm64, x86).
  2. Push the files to your device's temporary storage:
    adb push minicap /data/local/tmp/
    adb push minicap.so /data/local/tmp/
    
  3. Grant execution permissions:
    adb shell chmod +x /data/local/tmp/minicap
    

Python Code for Minicap

import subprocess
from PIL import Image
import io

def get_minicap_region(x, y, width=16, height=16):
    # Get screen size to pass to Minicap
    size_result = subprocess.check_output(
        ["adb", "shell", "wm", "size"]
    ).decode().strip()
    screen_size = size_result.split()[-1]  # Format: "WxH"
    
    # Run Minicap to capture the target region and output PNG data
    capture_cmd = [
        "adb", "exec-out",
        f"/data/local/tmp/minicap -P {screen_size}@{width}x{height}/{x},{y} -s"
    ]
    
    # Read the PNG output directly into memory
    png_data = subprocess.check_output(capture_cmd)
    return Image.open(io.BytesIO(png_data))

# Example usage
fast_region = get_minicap_region(200, 300)
fast_region.save("minicap_tiny_region.png")

Why Minicap is Better

  • It uses far less bandwidth and device resources than full screencaps, making it ideal for repeated captures.
  • Returns PNG data directly, so you don’t have to handle raw pixel byte math.

Honorable Mention: ADB + ImageMagick (If You Can Install Tools)

If you can install ImageMagick’s convert tool on your device (e.g., via Termux or rooted access), you can crop the screenshot directly on the device before pulling it:

import subprocess
from PIL import Image
import io

def get_convert_region(x, y, width=16, height=16):
    cmd = [
        "adb", "exec-out",
        f"screencap -p | convert -crop {width}x{height}+{x}+{y} - png:-"
    ]
    png_data = subprocess.check_output(cmd)
    return Image.open(io.BytesIO(png_data))

This is simple but less universal, as most stock Android devices don’t include ImageMagick by default.


内容的提问来源于stack exchange,提问作者user17

火山引擎 最新活动