/** * Construct with the given dimensions * * @param width the width * @param height the height * @throws VideoCaptureException */ public VideoCaptureComponent(int width, int height, String devName) throws VideoCaptureException { super(BoxLayout.Y_AXIS); this.setOpaque(false); this.width = width; this.height = height; final List<Device> devices = VideoCapture.getVideoDevices(); Video<MBFImage> vc = null; if (devices == null || devices.size() == 0) { currentDevice = null; final MBFImage[] frames = {new MBFImage(width, height, ColourSpace.RGB)}; frames[0].fill(RGBColour.RED); vc = new ArrayBackedVideo<MBFImage>(frames); } else { for (final Device d : devices) { if (d.getNameStr().contains(devName)) { currentDevice = d; break; } } if (currentDevice == null) currentDevice = devices.get(0); vc = new VideoCapture(width, height, currentDevice); } final JPanel videoDisplayPanel = new JPanel(); videoDisplayPanel.setMinimumSize(new Dimension(width, height)); videoDisplayPanel.setOpaque(false); display = VideoDisplay.createVideoDisplay(vc, videoDisplayPanel); add(videoDisplayPanel); final JPanel sourcesPanel = new JPanel(); sourcesPanel.setOpaque(false); sources = new JComboBox<String>(); sources.setOpaque(false); if (devices == null || devices.size() == 0) { sources.addItem("No cameras found"); sources.setEnabled(false); } else { for (final Device s : devices) sources.addItem(s.getNameStr()); } sources.setSelectedItem(currentDevice.getNameStr()); sources.addItemListener(this); sourcesPanel.add(sources); add(sourcesPanel); }
public static void main(String[] args) throws VideoCaptureException { if (args.length == 0) { int i = 0; System.out.println("Usage: VideoTest device [width [height [rate]]]"); for (final Device d : VideoCapture.getVideoDevices()) { System.out.println(i + "\t" + d.getNameStr()); i++; } return; } final Device dev = VideoCapture.getVideoDevices().get(Integer.parseInt(args[0])); final int width = args.length > 2 ? Integer.parseInt(args[1]) : 320; final int height = args.length > 2 ? Integer.parseInt(args[2]) : 240; final double rate = args.length > 2 ? Double.parseDouble(args[3]) : 30; final VideoCapture c = new VideoCapture(width, height, rate, dev); VideoDisplay.createVideoDisplay(c); }
@Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { final String item = (String) event.getItem(); final Device d = VideoCapture.getVideoDevices().get(sources.getSelectedIndex()); if (d.getNameStr().equals(item) && !currentDevice.equals(d)) { try { currentDevice = d; display.setMode(Mode.STOP); display.getVideo().close(); display.changeVideo(new VideoCapture(width, height, currentDevice)); display.setMode(Mode.PLAY); } catch (final VideoCaptureException e) { e.printStackTrace(); } } } }