adb 超快速截图bash脚本

17 篇文章 0 订阅
4 篇文章 0 订阅

这里21年的对之前的以下总结。bash脚本和Linux命令更熟练了,但是没有总结和记录。旧方式,截图很鸡肋,不太好用

电脑上的一些脚本
在这里插入图片描述
截图命令使用,原理是 adb exec-out
在这里插入图片描述

run-adb.bash

#adb 快捷命令

alias ar="adb reboot"

function adbConnect(){
    echo "Your connect is 192.168.$1"   # 这个 $1 必须要参考底下命令的下达
    adb connect 192.168.$1
}

function adbDisconnect(){
    adb disconnect
}

function adbShell(){
    adb shell
}

function devices(){
    adb devices
}

# 获取安卓设备数量
function getAdbDevicesCount(){
    no_dev=2 #没有设备的总行数   第一行: List of devices attached  第二行 空
    line="`adb devices | wc -l`"
    # echo "$line"
    echo $(($line-$no_dev))
}

#获取自定义格式设备名称 参数1: adb devices 设备ID index
function getFmtDeviceName(){
  if [ -n "$1" ]; then
    line=$1
    let "line++" #跳过1行
    deviceId="`adb devices | sed -n "${line}p" | awk '{printf $1"\n"}' `"     # name="`adb devices | sed -n "2p;${line}p" | awk '{printf NR ". " $1"\n"}' `" #简单列出设备ID
    manufacturer="`adb -s $deviceId shell getprop ro.product.manufacturer`"
    model="`adb -s $deviceId shell getprop ro.product.model`"
    version="`adb -s $deviceId shell getprop ro.build.version.release`"
    sdk="`adb -s $deviceId shell getprop ro.build.version.sdk`"
    name="${manufacturer} ${model} Android ${version} API ${sdk} Serial: ${deviceId} "
    # 去除某些设备后面携带回车符
    name=`echo ${name} | tr -d '\r'`
    echo ${name}
  else
    echo "requires an argument"
  fi
}

# 列出所有设备
function listFmtDevices(){
  count=`getAdbDevicesCount`
  index=1
  while(( $index<=count ))
  do
    name=`getFmtDeviceName ${index}`
    echo "${index}. ${name} "
    let "index++"
  done
}

# 获取设备ID 参数1: adb devices 设备ID index
function getFmtDeviceId(){
  if [ -n "$1" ]; then
    line=$1
    let "line++" #跳过1行
    deviceId="`adb devices | sed -n "${line}p" | awk '{printf $1"\n"}' `"
    # 去除某些设备后面携带回车符
    deviceId=`echo ${deviceId} | tr -d '\r'`
    echo ${deviceId}
  else
    echo "requires an argument"
  fi
}

# 安装apk
function apk(){
  if [ -n "$1" ]; then
    count=`getAdbDevicesCount`
    one_dev=1
    if [ $count -eq $one_dev ]
    then
      # 单设备
      name=`getFmtDeviceName 1`
      echo "install apk to devices: ${name}"
      adb install -r $1
    elif [ $count -gt $one_dev ] 
    then 
      # 多设备
      if [ -n "$2" ]; then
        # 带设备index
        index=$2
        deviceId=`getFmtDeviceId ${index}`
        name=`getFmtDeviceName ${index}`
        echo "install apk to devices: $name"
        adb -s $deviceId install -r $1
      else
        # 带apk文件路径
        echo "install apk to which devices?"
        listFmtDevices
        read -p "Enter: " index
        apk $1 $index
      fi
    else
      echo "no devices"
    fi
  else
    echo "apk requires an apkPath argument"
  fi
}

# 卸载apk
function uapk(){
  if [ -n "$1" ]; then
    count=`getAdbDevicesCount`
    one_dev=1
    if [ $count -eq $one_dev ]
    then
      # 单设备
      name=`getFmtDeviceName 1`
      echo "uninstall apk on $name"
      adb uninstall $1
    elif [ $count -gt $one_dev ] 
    then 
      # 多设备
      if [ -n "$2" ]; then
        # 带设备index
        index=$2
        deviceId=`getFmtDeviceId ${index}`
        name=`getFmtDeviceName ${index}`
        echo "uninstall apk on devices: $name"
        adb -s $deviceId uninstall $1
      else
        # 带apk文件路径
        echo "uninstall apk on which devices?"
        listFmtDevices
        read -p "Enter: " index
        uapk $1 $index
      fi
    else
      echo "no devices"
    fi
  else
    echo "uapk requires an pkg argument"
  fi
}

# 进入adb shell 环境
function as(){
  count=`getAdbDevicesCount`
  one_dev=1
  if [ $count -eq $one_dev ]
  then
    # 单设备
    name=`getFmtDeviceName 1`
    echo "${name} Last login: `date`"
    adb shell
  elif [ $count -gt $one_dev ] 
  then 
    # 多设备
    if [ -n "$1" ]; then
      # 带设备index
      index=$1
      deviceId=`getFmtDeviceId ${index}`
      name=`getFmtDeviceName ${index}`
      echo "${name} Last login: `date`"
      adb -s $deviceId shell
    else
      # 不带设备index
      echo "enter shell which devices?"
      listFmtDevices
      read -p "Enter: " index
      as $index
    fi
  else
    echo "no devices"
  fi
}
# tcpip 5555
alias a-tcpip="adb tcpip 5555"

# 截图
# adb exec-out screencap -p > screen.png
alias scap="adb exec-out screencap -p >"

function fcap(){
  # savepath=$(cd `dirname $0`; pwd)
  count=`getAdbDevicesCount`
  one_dev=1
  if [ $count -eq $one_dev ]
  then
    # 单设备
    name=`getFmtDeviceName 1`
    echo "${name} Last login: `date`"
    while true; 
    do
      
      DATE=`date +%Y%m%d%H%M%S`
      scap screen_${DATE}.png 
      echo "save screen_${DATE}.png success!"
      read -p 'Press any key to continue screencap ...'
    done
  elif [ $count -gt $one_dev ] 
  then 
    # 多设备
    if [ -n "$1" ]; then
      # 带设备index
      index=$1
      deviceId=`getFmtDeviceId ${index}`
      name=`getFmtDeviceName ${index}`
      echo "${name} Last login: `date`"
      while true; 
      do
        
        DATE=`date +%Y%m%d%H%M%S`
        adb -s $deviceId exec-out screencap -p > screen_${DATE}.png 
        echo "save screen_${DATE}.png success!"
        read -p 'Press any key to continue screencap ...'
      done
    else
      # 不带设备index
      echo "enter shell which devices?"
      listFmtDevices
      read -p "Enter: " index
      fcap $index
    fi
  else
    echo "no devices"
  fi

}

旧方式,很久没更新

对于Android Studio 截图功能不得吐槽一下,截图慢而不能连续截图,而且在某些android上不能截屏 .
具体使用adb 的 screencap 命令进行截图

Here

这里写图片描述

立马下载

截图bash脚本

#!/bin/bash
savepath=$(cd `dirname $0`; pwd)
function pause(){
        read -n 1 -p "$*" INP
        if [ [$INP != ''] ] ; then
                echo -ne '\b \n'
        fi
}

adb wait-for-device
echo 'wait-for-device'

result=`adb devices`

ipAddr=${result#*attached}
ipAddr=${ipAddr%%device*}
ipAddr=${ipAddr:1:19}
echo $ipAddr

pause 'Press any key to continue...'
while true; 
do
	pause 'Press any key to continue screencap ...'
	DATE=`date +%Y%m%d%H%M%S`

	adb -s ${ipAddr} shell screencap ./sdcard/screen_${DATE}.png 
	adb -s ${ipAddr} pull ./sdcard/screen_${DATE}.png ${savepath}
	adb -s ${ipAddr} shell rm ./sdcard/screen_${DATE}.png 
	echo "save screen_${DATE}.png to ${savepath} success!"
done

效果图

这里写图片描述

注意

允许上面脚本,当前电脑只允许一个adb设备连接。 回车即截图保存当前目录下

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值