📄 videoviewer.java
字号:
/* VideoViewer.java
*
* Responsible for creating a panel for operating display of videos.
*
* Copyright (C) 2002 by Frank McCown
* University of Arkansas at Little Rock
* July 2002
*/
import javax.swing.*;
import javax.media.*;
import com.sun.media.ui.*;
import javax.media.protocol.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import rowc.*;
public class VideoViewer extends JPanel
{
public static JCheckBox watchCheckBox;
public static JLabel lblInfo;
private static JComboBox locationsComboBox; // List of available web cams
public static VideoControlPanel videoControl;
private static ControllerVideoPanel videoPanel = null;
private VideoArchivePanel archivePanel;
private static String currentLocation = "";
private String archiveCurrentLocation = "";
// Indicates if video is currently being played in viewer
public static boolean videoPlaying = false;
// Indicates if video is currently being recorded
public static boolean recording = false;
public static rowc.CamManager camManager;
public static final int MIN_RECORDING_TIME = 1; // Min recording time in seconds
public static final int MAX_RECORDING_TIME = 30;
public VideoViewer()
{
// Register callback so addWebCam and removeWebCam methods are called by Server
CamControllerCallback callback = (new CamControllerCallbackImpl())._this(CamController.orb);
CamController.regObject.addCamController(CamController.hostName, callback);
MediaArchiveCallback mediaCallback = (new MediaArchiveCallbackImpl())._this(CamController.orb);
CamController.mediaStore.addMediaArchiveListener(CamController.hostName, mediaCallback);
setLayout(new GridBagLayout());
//lblItem = new JLabel();
videoPanel = new ControllerVideoPanel();
JScrollPane scroller = new JScrollPane(videoPanel);
lblInfo = new JLabel();
lblInfo.setForeground(Color.red);
locationsComboBox = new JComboBox();
populateLocationsComboBox();
locationsComboBox.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
//stopCamViewer();
if (videoPlaying)
watchCheckBox.setSelected(false);
Object obj = locationsComboBox.getSelectedItem();
if (obj != null)
currentLocation = obj.toString();
else
currentLocation = "";
System.out.println("Selected location " + currentLocation);
lblInfo.setText("");
}
}
}
);
// Movement checkbox
watchCheckBox = new JCheckBox("Watch", false);
watchCheckBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
//System.out.println("Watch itemStateChanged called.");
if (e.getStateChange() == ItemEvent.SELECTED)
{
// Force to repaint?
Object obj = locationsComboBox.getSelectedItem();
if (obj != null)
{
currentLocation = obj.toString();
if (!startCamViewer())
watchCheckBox.setSelected(false);
}
else
{
// This could happen if location box is empty
currentLocation = "";
showError("There is no location selected.");
watchCheckBox.setSelected(false);
}
}
else if (e.getStateChange() == ItemEvent.DESELECTED)
stopCamViewer();
}
});
archivePanel = new VideoArchivePanel();
videoControl = new VideoControlPanel();
// GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty,
// int anchor, int fill, Insets insets, int ipadx, int ipady)
// Add components to container
add(scroller, new GridBagConstraints(0, 0, 1, 4, 1.0, 0.9,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 217, 202));
add(new JLabel("Location:"), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(locationsComboBox, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
add(watchCheckBox, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(videoControl, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(lblInfo, new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(archivePanel, new GridBagConstraints(0, 4, 2, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 0, 0, 5), 0, 0));
}
public void startRecording(int maxTime)
{
// Start recording and have it sent here
if (!getCamManagerForLocation())
return;
recording = true;
lblInfo.setText("Recording...");
// Register callback so newRecording method is called
CamVideoCallback callback = (new CamVideoCallbackImpl())._this(CamController.orb);
if (!camManager.startRec(maxTime, callback))
{
showError("Unable to start recording... Cam Processor is busy.");
lblInfo.setText("");
return;
}
}
private void stopRecording()
{
// Tell cam manager to stop recording and store video on Server.
camManager.stopRec();
}
public static void terminate()
{
// Notify Server that we're terminating so it takes us off its notification list
CamController.regObject.removeCamController(CamController.hostName);
}
private boolean startCamViewer()
{
videoPlaying = false;
if (!getCamManagerForLocation())
return false;
lblInfo.setText("Loading media...");
try
{
// Register callback so videoAvailable method is called
CamVideoCallback callback = (new CamVideoCallbackImpl())._this(CamController.orb);
// Tell cam to start RTP transmission to this computer.
if (!camManager.startTransmission(CamController.hostName, callback))
{
showError("Could not watch because Cam Controller is busy.");
watchCheckBox.setSelected(false);
lblInfo.setText("");
return false;
}
}
catch (org.omg.CORBA.TRANSIENT e)
{
e.printStackTrace();
showError("The web cam is unavailable. Unable to view video.");
lblInfo.setText("");
return false;
}
return true;
}
public static void stopCamViewer()
{
System.out.println("stopCamViewer");
try
{
if (camManager != null && videoPlaying)
{
System.out.println("STOPPING TRANSMISSION");
videoPanel.stop();
camManager.stopTransmission();
videoPlaying = false;
}
}
catch (org.omg.CORBA.TRANSIENT ex)
{
System.out.println(ex);
showError("It appears that the CamManager being used is no longer available.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static boolean getCamManagerForLocation()
{
try
{
// Find CamManager for specific location; Obj ref is stored in Naming Service
org.omg.CosNaming.NameComponent[] camManagerNC = new org.omg.CosNaming.NameComponent[1];
String camName = "CamManager_" + currentLocation;
camManagerNC[0] = new org.omg.CosNaming.NameComponent(camName,"");
camManager = rowc.CamManagerHelper.narrow(CamController.nc.resolve(camManagerNC));
}
catch (org.omg.CosNaming.NamingContextPackage.NotFound ex)
{
showError("Error retrieving media from the selected location.\n" +
"Make sure the cam software is running for the selected web cam.");
return false;
}
catch (Exception ex)
{
ex.printStackTrace();
showError("Error retrieving media from the selected location.");
return false;
}
return true;
}
private void populateLocationsComboBox()
{
// Location combo box- Retrieve locations from DB
if (CamController.connectedToServer)
{
try
{
System.out.println("Retrieving locations from server...");
String[] loc = CamController.regObject.getLocations();
// If first slot is empty, there are no locations available
if (!loc[0].equals(""))
{
locationsComboBox = new JComboBox(loc);
currentLocation = loc[0];
}
}
catch (Exception e)
{
System.out.println("Unable to get locations from Server.");
lblInfo.setText("No locations available.");
}
}
}
private static boolean locationIsKnown(String location)
{
// Return true if location is already known about (in combo box)
for (int i = 0; i < locationsComboBox.getItemCount(); i++)
{
String loc = (String) locationsComboBox.getItemAt(i);
if (location.compareToIgnoreCase(loc) == 0)
return true;
}
return false;
}
public static void showError(String error)
{
JOptionPane.showMessageDialog(null, error, "Cam Controller Error",
JOptionPane.ERROR_MESSAGE);
}
/***************************************************************************
*
* Inner class responsible for GUI for controlling video portion of web cam
*
***************************************************************************/
class VideoControlPanel extends JPanel
{
private JButton openFileButton, startButton, stopButton;
private JTextField maxRecTimeTextField;
private FileDialog fd = null;
public VideoControlPanel()
{
setLayout(new GridBagLayout());
ControlButtonHandler handler = new ControlButtonHandler();
startButton = new JButton("Start");
startButton.addActionListener(handler);
stopButton = new JButton("Stop");
stopButton.addActionListener(handler);
// Allow user to enter maximum number of seconds to record
maxRecTimeTextField = new JTextField("4", 4);
openFileButton = new JButton("Open File...");
openFileButton.addActionListener(handler);
add(new JLabel("Recording:"), new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(startButton, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 10, 0), 0, 0));
add(stopButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 10, 0), 0, 0));
add(new JLabel("Max recording time:"), new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
add(maxRecTimeTextField, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 10, 0), 0, 0));
add(new JLabel("Seconds"), new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 3, 10, 0), 0, 0));
add(openFileButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
public int getMaxRecTime()
{
int time;
try
{
time = Integer.parseInt(maxRecTimeTextField.getText());
}
catch (NumberFormatException e)
{
time = MIN_RECORDING_TIME - 1;
}
return time;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -