001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.campusview; 031 032import java.awt.Color; 033import java.awt.Dimension; 034import java.awt.Font; 035import java.awt.GridBagConstraints; 036import java.awt.GridBagLayout; 037import java.awt.event.ActionEvent; 038import java.awt.event.ActionListener; 039import java.util.List; 040 041import javax.swing.JComboBox; 042import javax.swing.JLabel; 043import javax.swing.JPanel; 044 045import org.openimaj.image.MBFImage; 046import org.openimaj.video.VideoDisplay; 047import org.openimaj.video.capture.Device; 048import org.openimaj.video.capture.VideoCapture; 049import org.openimaj.video.capture.VideoCaptureException; 050 051/** 052 * A JPanel that displays the output of a camera 053 * 054 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 055 */ 056public class CaptureComponent extends JPanel { 057 private static final long serialVersionUID = 1L; 058 059 private static final List<Device> devices = VideoCapture.getVideoDevices(); 060 061 private JComboBox<Object> comboBox; 062 private JPanel panel; 063 private JLabel label; 064 065 private VideoDisplay<MBFImage> display; 066 067 private int capHeight = 320; 068 private int capWidth = 240; 069 private double capRate = 25; 070 071 private int defaultWidth = 320; 072 private int defaultHeight = 240; 073 074 /** 075 * Create the panel. 076 */ 077 public CaptureComponent() { 078 this(640, 480, 1); 079 } 080 081 /** 082 * Create the panel. 083 * 084 * @param capWidth 085 * @param capHeight 086 * @param capRate 087 */ 088 public CaptureComponent(int capWidth, int capHeight, double capRate) 089 { 090 this.capWidth = capWidth; 091 this.capHeight = capHeight; 092 this.capRate = capRate; 093 094 setOpaque(false); 095 setLayout(new GridBagLayout()); 096 097 final GridBagConstraints gbc = new GridBagConstraints(); 098 gbc.gridx = 0; 099 gbc.gridy = 0; 100 gbc.fill = GridBagConstraints.HORIZONTAL; 101 102 label = new JLabel("Camera #1"); 103 label.setForeground(Color.WHITE); 104 label.setFont(new Font("Lucida Grande", Font.PLAIN, 16)); 105 label.setBounds(6, 6, 124, 16); 106 add(label, gbc); 107 108 gbc.gridy++; 109 gbc.fill = GridBagConstraints.HORIZONTAL; 110 gbc.weighty = 0; 111 comboBox = new JComboBox<Object>(); 112 comboBox.setBounds(6, 286, 320, 27); 113 add(comboBox, gbc); 114 115 gbc.gridy++; 116 gbc.fill = GridBagConstraints.BOTH; 117 gbc.weighty = 1; 118 panel = new JPanel(); 119 panel.setOpaque(false); 120 panel.setBounds(6, 34, defaultWidth, defaultHeight); 121 panel.setMaximumSize(new Dimension(defaultWidth, defaultHeight)); 122 add(panel, gbc); 123 124 initSrcList(); 125 } 126 127 /** 128 * Set the title of the component 129 * 130 * @param title 131 */ 132 public void setTitle(String title) { 133 label.setText(title); 134 } 135 136 /** 137 * @return the title of the component 138 */ 139 public String getTitle() { 140 return label.getText(); 141 } 142 143 private void initSrcList() { 144 comboBox.addItem("None"); 145 146 for (final Device d : devices) 147 comboBox.addItem(d); 148 149 comboBox.addActionListener(new ActionListener() { 150 151 @Override 152 public void actionPerformed(ActionEvent e) { 153 setupVideo(); 154 } 155 156 }); 157 158 comboBox.setSelectedItem(0); 159 } 160 161 private void setupVideo() { 162 if (comboBox.getSelectedItem().equals("None")) 163 return; 164 165 final Device dev = (Device) comboBox.getSelectedItem(); 166 167 if (display != null) { 168 ((VideoCapture) display.getVideo()).stopCapture(); 169 panel.removeAll(); 170 } 171 172 System.out.println(dev); 173 174 try { 175 display = VideoDisplay.createVideoDisplay(new VideoCapture(capWidth, capHeight, capRate, dev), panel); 176 } catch (final VideoCaptureException e) { 177 throw new RuntimeException(e); 178 } 179 180 revalidate(); 181 repaint(); 182 } 183 184 /** 185 * @return the capHeight 186 */ 187 public int getCapHeight() { 188 return capHeight; 189 } 190 191 /** 192 * @param capHeight 193 * the capHeight to set 194 */ 195 public void setCapHeight(int capHeight) { 196 this.capHeight = capHeight; 197 setupVideo(); 198 } 199 200 /** 201 * @return the capWidth 202 */ 203 public int getCapWidth() { 204 return capWidth; 205 } 206 207 /** 208 * @param capWidth 209 * the capWidth to set 210 */ 211 public void setCapWidth(int capWidth) { 212 this.capWidth = capWidth; 213 setupVideo(); 214 } 215 216 /** 217 * @return the capRate 218 */ 219 public double getCapRate() { 220 return capRate; 221 } 222 223 @Override 224 public int getWidth() 225 { 226 if (display != null) 227 return display.getScreen().getWidth(); 228 return defaultWidth; 229 } 230 231 @Override 232 public int getHeight() 233 { 234 if (display != null) 235 return display.getScreen().getHeight(); 236 return defaultHeight; 237 } 238 239 /** 240 * @param capRate 241 * the capRate to set 242 */ 243 public void setCapRate(double capRate) { 244 this.capRate = capRate; 245 setupVideo(); 246 } 247 248 /** 249 * @return the current frame 250 */ 251 public MBFImage getCurrentFrame() { 252 if (display != null) 253 return display.getVideo().getCurrentFrame(); 254 return null; 255 } 256}