1
1

More than 1 year has passed since last update.

adbで端末のスクリーンショットを撮っていい感じにリサイズして保存するコマンド

Last updated at Posted at 2021-12-08

Macだったら、以下のコマンド一発でできました。Windowsの方はゴメンナサイ…

以下は最大サイズ640でscreen.pngにスクリーンショットを保存するコマンドです。

adb exec-out screencap -p > screen.png && sips -Z 640 screen.png

用途に応じてサイズやファイル名は適当に調整してください。

各コマンドについて

sips

画像ファイルの処理ができるコマンドです。今回使用した-Zは、高さまたは幅の大きい方が指定した値になるように、アスペクト比を保ってリサイズするオプションです。
他にもいろいろなことができそうです。

  Usages:
    sips [image-functions] imagefile ... 
    sips [profile-functions] profile ... 

  Profile query functions: 
    -g, --getProperty key 
    -X, --extractTag tag tagFile 
        --verify 
    -1, --oneLine 

  Image query functions: 
    -g, --getProperty key 
    -x, --extractProfile profile 
    -1, --oneLine 

  Profile modification functions: 
    -s, --setProperty key value 
    -d, --deleteProperty key 
        --deleteTag tag 
        --copyTag srcTag dstTag 
        --loadTag tag tagFile 
        --repair 
    -o, --out file-or-directory 

  Image modification functions: 
    -s, --setProperty key value 
    -d, --deleteProperty key 
    -e, --embedProfile profile 
    -E, --embedProfileIfNone profile 
    -m, --matchTo profile 
    -M, --matchToWithIntent profile intent 
        --deleteColorManagementProperties 
    -r, --rotate degreesCW 
    -f, --flip horizontal|vertical 
    -c, --cropToHeightWidth pixelsH pixelsW 
        --cropOffset offsetY offsetH 
    -p, --padToHeightWidth pixelsH pixelsW 
        --padColor hexcolor 
    -z, --resampleHeightWidth pixelsH pixelsW 
        --resampleWidth pixelsW 
        --resampleHeight pixelsH 
    -Z, --resampleHeightWidthMax pixelsWH 
    -i, --addIcon 
        --optimizeColorForSharing 
    -o, --out file-or-directory 
    -j, --js file 

  Other functions: 
        --debug           Enable debugging output
    -h, --help            Show help
    -H, --helpProperties  Show help for properties
        --man             Generate man pages
    -v, --version         Show the version
        --formats         Show the read/write formats

adb

USB接続されたAndroid端末を操作するコマンドです。今回使用したexec-outは、Android端末上で指定したコマンドを実行して、その結果(Android端末上の標準出力)をPC側の標準出力に流すコマンドです。
その他、adbでできることを知りたい方は公式ドキュメントを読むと良いです。ちゃんと日本語化されていてありがたい。

screencap

screencapはAndroid端末上で実行されるコマンドで、画面のキャプチャを行います。
screencap単体で実行すると標準出力にキャプチャ結果が流れます。-pを指定するとそのデータがPNG形式になります。
その他の詳細な情報はscreencap -hで確認できます。

usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the physical display ID to capture
       see "dumpsys SurfaceFlinger --display-id" for valid display IDs.
If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

エイリアスを定義する

毎回

adb exec-out screencap -p > screen.png && sips -Z 640 screen.png

のようなコマンドを打つのは大変なので、エイリアスを作りました。$HOME/.zshrcに以下のような内容を付け足します。

$HOME/.zshrc
alias cap='adb exec-out screencap -p > screen.png && sips -Z 640 screen.png'

これで、capと3文字打つだけでキャプチャが取れるようになりました。

$ cap
/Users/niu/screen.png
  /Users/niu/screen.png

なお、これだと毎回screen.pngというファイル名になるので、複数回実行するとどんどん上書きしていってしまいます。

ファイル名の末尾にタイムスタンプを付けてファイルを上書きしないように改良したバージョンも併せて載せておきます。

HOME/.zshrc
cap() {
    currentTime=`date +%s`
    cap2 "$currentTime"
}

cap2() {
    adb exec-out screencap -p > screen_"$1".png && sips -Z 640 screen_"$1".png
}

これで、capを実行するたびに異なるファイル名でキャプチャが作成されるので(しかもファイル名でソートすれば時系列順に並ぶ)、便利です!

$ cap
/Users/niu/screen_1653301253.png
  /Users/niu/screen_1653301253.png
$ cap
/Users/niu/screen_1653302150.png
  /Users/niu/screen_1653302150.png
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1