Using a combination of open-source software tools like adb, v4l2, and scrcpy, it is possible to utilize an Android phone's camera as a webcam for various applications.
Since I am using arch with pacman as package manager, my command would be:
sudo pacman -S scrcpy v4l2loopback-dkms ffmpeg android-tools
This will vary depending on your distrobution.
You can either do this wirelessly or using usb cable: First of all make sure to enable usb debugging in developpers settings.
- Using usb cable: Straight forward method, plug your phone with your usb cable. There should be a popup asking for permision for usb debugging, accept it.
- Using wifi: First with the phone connnected using the usb cable write:
adb tcpip 5555 #port number is optional
Then have your computer and phone connected on the same network (common wifi or hotspot). Find your phone's ip address on the network then do:
adb connect {YOUR PHONE IP}:5555 #change port to what you choose earlier
Simply load the module to the kernel by:
sudo modprobe v4l2loopback
Find the last number presented in the available devicecs using:
ls /dev/video*
Or be fancy and use v4l2-utils:
v4l2-ctl --list-devices
Using scrcpy, we can stream our camera output (or display ) to v4l2 by:
scrcpy --video-source=camera --v4l2-sink=/dev/videoN # replace N with the ID you found earlier
scrcpy --video-source=camera --no-audio --camera-size=640x480 --no-video-playback --v4l2-sink=/dev/videoN -b 2M # You can use lots of options, check scrcpy docs
We will be using ffmpeg command ffplay to preview our camera output:
ffplay /dev/videoN
Have fun!