/** * Creates new webcam panel which display image from camera in you your * Swing application. If panel size argument is null, then image size will * be used. If you would like to fill panel area with image even if its size * is different, then you can use {@link #setFillArea(boolean)) method to * configure this. * * @param webcam the webcam to be used to fetch images * @param size the size of panel * @param start true if webcam shall be automatically started * @see WebcamPanel#setFillArea(boolean) */ public WebcamPanel(Webcam webcam, Dimension size, boolean start) { if (webcam == null) { throw new IllegalArgumentException( String.format( "Webcam argument in %s constructor cannot be null!", getClass().getSimpleName())); } this.webcam = webcam; this.webcam.addWebcamListener(this); repainter.setName(String.format("%s-repainter", webcam.getName())); if (size == null) { setPreferredSize(webcam.getViewSize()); } else { setPreferredSize(size); } if (start) { if (!webcam.isOpen()) { webcam.open(); } repainter.start(); } }
@Override public void webcamClosed(WebcamEvent we) { if (repainter != null) { if (repainter.isAlive()) { try { repainter.join(1000); } catch (InterruptedException e) { throw new WebcamException("Thread interrupted", e); } } repainter = null; } }
@Override public void webcamOpen(WebcamEvent we) { if (repainter == null) { repainter = new Repainter(); repainter.start(); } setPreferredSize(webcam.getViewSize()); }
/** Open webcam and start rendering. */ public void start() { if (started.compareAndSet(false, true)) { starting = true; if (repainter == null) { repainter = new Repainter(); } repainter.start(); webcam.open(); starting = false; } }