Switching Cameras with code


Our team is thinking of using two cameras on our robot, and would like to be able to switch which camera is being displayed to the smart dashboard with the press of a button.

As of this point in time, we are not using vision tracking or anything complicated like that.

Switching Between Cameras?

Take a look at Camera Server - we have used it before and it is super easy to display to the smart dashboard. The documentation is here. The methods you are probably looking for is the startAutomaticCapture() function which can take in a number or ID for the camera stream. On the dashboard, you can change which camera stream to listen to.
If you want to dynamically control the stream from the RIO, you can also use the putVideo() function where you can manually send camera data. Simply use the VideoCapture class from OpenCV for your two cameras and change which frame is being sent using putVideo(). Hope that helps!


It’s not necessary to use OpenCV for this (and the OpenCV VideoCapture class is a lot less reliable than CameraServer’s video capture). See the CameraServer documentation for switching the driver views for how to do it with CameraServer (I recommend the “keeping streams open” approach for the speediest switching).


Is there any equivalent to this for Java?


C++ and Java are very similar. Here’s a quick Java conversion.

UsbCamera camera1;
UsbCamera camera2;
VideoSink server;
Joystick joy1 = new Joystick(0);
boolean prevTrigger = false;
void robotInit() {
  camera1 = CameraServer.getInstance().startAutomaticCapture(0);
  camera2 = CameraServer.getInstance().startAutomaticCapture(1);
  server = CameraServer.getInstance().addServer("Switched camera");
void teleopPeriodic() {
  if (joy1.getTrigger() && !prevTrigger) {
    System.out.println("Setting camera 2");
  } else if (!joy1.getTrigger() && prevTrigger) {
    System.out.println("Setting camera 1");
  prevTrigger = joy1.getTrigger();

Robot OpenCV allocation crash with camera code
Displaying 2 USB Cameras

Thank you so much! I’ve been looking for a simple way to do this for a while, and this looks like what I’ve been looking for.


Is it that the docs are hard to navigate/find, or that the language of the example was C++? We do want to improve the team experience.


I think It’s mostly that I’m just dumb

Also, one last question. How do you view the server on the smart dashboard?


Do View | Add | CameraServer Stream Viewer. Right click on that, select Properties, then change the Camera Choice to the name passed to addServer.

I realized the above code is incorrect if you want access to both the individual streams as well as the switched stream, as it should use “addServer(name)” rather than “getServer”. If you use “getServer()” the switched stream is the camera 0 stream (the unswitched camera 0 is no longer available).


(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)