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
に以下のような内容を付け足します。
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
というファイル名になるので、複数回実行するとどんどん上書きしていってしまいます。
ファイル名の末尾にタイムスタンプを付けてファイルを上書きしないように改良したバージョンも併せて載せておきます。
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