背景
因为 UI 验收等需求的存在,我们经常需要将测试机上的屏幕截屏然后发送给其他小伙伴。所以需要快捷高效的方式来获取屏幕截图。
思路
- 以华为手机为例,华为系统截屏之后,图片保存在 /sdcard/Pictures/Screenshots/目录下,所以每次截屏之后,可以通过adb pull的方式把手机截屏导出,然后发送出去
- 利用 adb 自带的截屏命令直接截屏,并导出到电脑上,然后发送出去
这里采用方案2。
实现
在 ~/.bash_profile 或者 ~/.zshrc 文件中添加:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | file-to-clipboard() {
 osascript \
 -e 'on run args' \
 -e 'set the clipboard to POSIX file (first item of args)' \
 -e end \
 "$@"
 }
 
 
 alias ftc='file-to-clipboard'
 
 
 phoneshot() {
 copy_to_clipboard=0
 if [ $# -eq 0 ]
 then
 :
 else
 if [[ $1 == '-c' ]] || [[ $1 == '--copy' ]]; then
 copy_to_clipboard=1
 elif [[ $1 == '-h' ]] || [[ $1 == '--h' ]]; then
 echo 'usage: '
 echo 'phoneshot/pshot     将手机截屏并保存为 ~/Downlodas/screenshots.png'
 echo 'phoneshot/pshot -c  将手机截屏并保存,然后复制图片到剪贴板'
 return
 fi
 fi
 
 name="screenshot.png"
 adb shell screencap -p /sdcard/$name
 out_file=~/Downloads/$name
 adb pull /sdcard/$name $out_file
 adb shell rm /sdcard/$name
 echo "save screenshot to $out_file"
 
 if [[ $copy_to_clipboard == 1 ]]; then
 ftc $out_file
 echo "copy screenshot file to clipboard"
 fi
 }
 
 
 alias pshot='phoneshot'
 
 | 
使用 source ~/.bash_profile 或者 source ~/.zshrc 重新加载 profile, 让自定义命令生效
使用
- phoneshot/pshot
 将手机截屏并保存为 ~/Downlodas/screenshots.png
- phoneshot/pshot -c
 将手机截屏并保存,然后复制图片到剪贴板
参考
- How to use terminal to copy a file to the clipboard?
- Using ADB to capture the screen