Hey CD,
I updated my old code that worked with any type of camera to be a bit more extendable, but it only works with USBCameras now.
Imports:
import java.util.ArrayList;
import com.ni.vision.NIVision;
import com.ni.vision.NIVision.Image;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.vision.USBCamera;
Variables:
/**
* The image to push to the CameraServer
*/
private Image frame;
/**
* The list of all attached cameras
*/
private ArrayList<USBCamera> cams;
/**
* The index of the current camera we are looking at
*/
private int currCam;
/**
* The current camera we are viewing.
*/
private USBCamera cam;
/**
* The maximum fps for all of the cameras
*/
private final int MAX_FPS = 15;
/**
* The quality of image to push back to the driver station. Lower numbers save more bandwidth (0-100)
*/
private final int QUALITY = 10;
/**
* Time to sleep after changing camera views. This is to prevent errors as USBCamera.startCapture() returns before it is ready to be seen
*/
private final long SLEEP_TIME = 100;
init Code:
cams = new ArrayList<USBCamera>();
frame = NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_RGB, 0);
currCam = 0;
CameraServer.getInstance().setQuality(QUALITY);
for(String s: /*Array of camera names*/){
addCamera(s);
}
cam = cams.get(currCam);
cam.openCamera();
cam.startCapture();
addCamera method:
/**
* Adds the camera to our list to switch between and sets the FPS max
* @param camName The name of the camera
*/
private void addCamera(String camName){
USBCamera temp = new USBCamera(camName);
temp.setFPS(MAX_FPS);
cams.add(temp);
temp = null;
}
Loop code:
cams.get(currCam).getImage(frame);
CameraServer.getInstance().setImage(frame);
Switching Method:
/**
* Switch to the next camera in our ArrayList
*/
public void switchCamera(){
try{
cam.stopCapture();
cam.closeCamera();
currCam++;
currCam %= cams.size();
cam = cams.get(currCam);
cam.openCamera();
cam.startCapture();
Thread.sleep(SLEEP_TIME);
}catch(Exception e){
e.printStackTrace();
}
}
As always, feel free to post below or send me a PM if you need help!