Displaying 2 USB Cameras

That page is written in c++. Is there anything for Java?

See Switching Cameras with code for a Java equivalent. They really are very very similar, which is why screensteps often only has one or the other.


Thank you! That helps a lot.

Just curious - what sort of camera switching times are you all getting via code?
Our programmers told us (electrical) that it could take up to a couple of seconds and we didn’t second guess them.
Built a USB relay that toggles input to the RoboRio USB using a signal out of the DIO.
It’s still some software, but the relay switch is just about instantaneous.

Are you talking about a USB physical switch that disconnects the camera?? That can’t possibly be faster than keeping both cameras connected and using software to switch, as physically disconnecting means the camera would need to renegotiate the USB protocol etc. While the “default” software behavior is to disconnect the cameras, causing a couple second delay when switching as the camera reconnects, my post linked to above as well as the second example in screensteps keeps both cameras open and connected, which results in a near-instantaneous switch.

A post was merged into an existing topic: VSCode WPILib “test” feature

Good question and points, because of my incomplete description.
We went on Amazon and got some off the shelf small relays that switch two USB cameras connected (via the relays) to one USB on the Roborio.
As far as the software knows, there’s only one camera connected.
We originally thought we would just switch the two USB data lines but we do the power and ground too.

I’m not convinced there’s any advantage to doing this but the programming team claimed software had a switching lag…this setup is an instant switch.


It would be interesting to see what “dmesg” outputs when you switch the cameras. I would expect to see it disconnect one camera and connect the new one.

I’ll see about getting info on messages when the cameras are switched via the relay module…might be a little while before I ask programmers for that , bag day is looming!

I tried to add the code to my project but I am getting errors like “Type mismatch: cannot convert from MjpegServer to CameraServer”

I followed the toggleable switch code, but I’m not sure how to view the stream from the server on shuffleboard. How do can I view the stream?

The switched stream will appear as a virtual “camera” in the list of cameras in Shuffleboard, just like the real two cameras.

Are you talking about the Java code? There’s no use of the MjpegServer class explicitly in that code. So I’m going to need more information (your code, what errors you’re getting) to help.

@Peter_Johnson I can see my two created cameras with name “frontcam” and “backcam” but don’t see anything besides that under the cameraserver tab.

Here is my code:

You have two Robot.java’s in that repository… are you sure you’re running the right one?

Yeah, I’m positive. New to github, but ignore everything in the ‘main’ folder. The only files that interact with the cameras are Controls and the Robot.java outside the folder.

1 Like

It looks like it won’t appear in the camera list until it has a source assigned. Try setting an initial source for the switched stream. E.g. after the setConnectionStrategy calls, do server.setSource(frontcam);

If this works, I’ll edit my post above to correct this.

Unfortunately didn’t work.

Upon further investigation, it looks like this approach to switched cameras doesn’t work with the dashboards as well as I thought it would, as it doesn’t actually create a virtual camera, just a server. Looking into potential workarounds…

1 Like

This was fixed in 2019.3.2 by adding the addSwitchedCamera function to CameraServer.

1 Like