?? camprocessor.java
字號(hào):
/* CamProcessor.java * * Application that interfaces with a web cam and allows remote CamControllers * to control the web cam. * * Optional startup arguments: * -loc used for overriding location name. Default is computer's host name. * -file used to play a local QuickTime movie file * * Example: * java CamProcessor -loc ComputerLab -file "C:\movies\loop.mov" * * would set the location to "COMPUTERLAB" and play loop.mov repeatedly. * * Copyright (C) 2002 by Frank McCown * University of Arkansas at Little Rock * July 2002 */import org.omg.CosNaming.*;import org.omg.CosNaming.NamingContextPackage.*;import org.omg.PortableServer.*;import java.awt.event.*;import javax.swing.*;import java.awt.*;import java.io.*;import javax.media.*;import javax.media.protocol.*; import rowc.*;public class CamProcessor extends JFrame { private final int DEFAULT_WINDOW_WIDTH = 470; private final int DEFAULT_WINDOW_HEIGHT = 370; private static final String PROPERTY_FILE = "project.conf"; private static String fileSpecified = null; // Used for movie files (when no web cam is available) public static boolean quitting = false; private static NameComponent camManagerNC[]; private static NamingContext ncRef; public static Registration regObject; // Ref to remote object private static CamManagerImpl camManager; // Created servant private JButton btnGetFile; public static JLabel lblInfo; private JLabel lblLocation; private JCheckBox viewVideoCheckBox; public static JScrollPane videoScrollPane; public static VideoTransmitter vt; public static TransmitterThread transThread; private ProcessorVideoPanel videoPanel; // Panel that displays live video public static DataSource dataSource = null; // Connected to web cam public static String location = ""; // Unique identifier for cam processor // Constructor private CamProcessor() { super("Cam Processor"); videoPanel = new ProcessorVideoPanel(); // Tell Server about us so he'll add us to his list if (regObject != null) regObject.addWebCam(location); // Start thread to produce video for video player ViewingThread vThread = new ViewingThread(); vThread.start(); Container c = getContentPane(); c.setLayout(new GridBagLayout()); videoScrollPane = new JScrollPane(videoPanel); //for loading two frames viewVideoCheckBox = new JCheckBox("View Video", true); viewVideoCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { // Show/hide video panel when selected/deselected if (e.getStateChange() == ItemEvent.SELECTED) videoPanel.setVisible(true); else if (e.getStateChange() == ItemEvent.DESELECTED) videoPanel.setVisible(false); } }); lblLocation = new JLabel("Location: " + location); lblInfo = new JLabel(); lblInfo.setForeground(Color.red); c.add(videoScrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 217, 202)); c.add(viewVideoCheckBox, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); c.add(lblLocation, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); c.add(lblInfo, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); setSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT); show(); } private void showError(String error) { JOptionPane.showMessageDialog(this, error, "CamProcessor Error", JOptionPane.ERROR_MESSAGE); } private static void activateCamManager(org.omg.CORBA.ORB orb) { try { // get reference to rootpoa & activate the POAManager POA rootPOA = (POA)orb.resolve_initial_references("RootPOA"); rootPOA.the_POAManager().activate(); // Create the servant camManager = new CamManagerImpl(location); rowc.CamManager href = camManager._this(orb); String host = regObject.getServerHostName(); camManager.setServerName(host); // Get the root naming context ncRef = NamingContextHelper.narrow( orb.resolve_initial_references("NameService")); // Bind the Object Reference in Naming String camName = "CamManager_" + location; camManagerNC = new NameComponent[1]; camManagerNC[0] = new NameComponent(camName,"");//System.out.println("Registering " + camName + " with Naming Service."); ncRef.rebind(camManagerNC, href); } catch (Exception e) { System.out.println("Error trying to activate CamManager implementation."); e.printStackTrace(); } } public static void transmitVideo(String hostName) { // Create new thread for trasmitter going to new location System.out.println("Starting new thread for " + hostName); transThread = new TransmitterThread(hostName); Thread thisThread = Thread.currentThread();/* try { thisThread.sleep(2500); // WHY WAITING? } catch (InterruptedException e) {}*/ transThread.start(); } public static void stopTransmitVideo() { transThread = null; } public static void main(String[] args) { // Attempt to read location from command line for (int i=0; i < args.length; i++) { if (args[i].equals("-loc")) { if(i+1 >= args.length) { System.err.println("Argument expected for " + args[i]); System.exit(0); } StringBuffer loc = new StringBuffer(args[i+1]); location = loc.toString().toUpperCase(); i++; // Skip "-loc" next iteration } else if (args[i].equals("-file")) { if(i+1 >= args.length) { System.err.println("Argument expected for " + args[i]); System.exit(0); } fileSpecified = new StringBuffer(args[i+1]).toString(); i++; // Skip "-file" next iteration } } // Get ip addr for this computer to be used for creating a receiver try { java.net.InetAddress ia = java.net.InetAddress.getLocalHost(); String ipAddr = ia.getHostAddress(); if (location.length() == 0) location = ia.getHostName().toUpperCase(); // Use host name for location unless over-ridden } catch (java.net.UnknownHostException ex) { ex.printStackTrace(); } java.util.Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB"); props.put("org.omg.CORBA.ORBSingletonClass", "com.ooc.CORBA.ORBSingleton"); try { // Read properties from project property file that indicates location // of Name and Event Services. FileInputStream f = new FileInputStream(PROPERTY_FILE); props.load(f); f.close(); //java.net.URL url = new java.net.URL(PROPERTY_FILE); //props.load(url.openStream()); } catch (Exception e) { System.out.println("Could not load properties from " + PROPERTY_FILE); e.printStackTrace(); } final org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, props); // Get objects by name try { // Get naming service NamingContext nc = NamingContextHelper.narrow( orb.resolve_initial_references("NameService")); // Get remote object using Naming Service NameComponent[] ncArray = new NameComponent[1]; ncArray[0] = new NameComponent("Registration",""); regObject = rowc.RegistrationHelper.narrow(nc.resolve(ncArray)); // Activate CamManager activateCamManager(orb); } catch (org.omg.CORBA.TRANSIENT e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Unable to locate Naming Service.\nMake sure Naming Service is running.", "Cam Processor Error", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog( null,"Error finding Registration.\nMake sure the CORBA server is running.", "Cam Processor Error", JOptionPane.ERROR_MESSAGE); } final CamProcessor app = new CamProcessor(); app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { quitting = true; // Setting this will cause transmitter thread to quit try { camManager.stopRec(); // Deregister with server since we'll no longer be available and unbind from // Naming Service or CamManager for this location will remain regObject.removeWebCam(location); ncRef.unbind(camManagerNC); // Stop and close the live video window app.videoPanel.stop(); } catch (Exception ex) { ex.printStackTrace(); } ((com.ooc.CORBA.ORB)orb).destroy(); System.exit(0); } }); } // Internal class class ViewingThread extends Thread { public void run() { // Create a DataSource that will be cloned by the VideoTransmitter try { DataSource ds; MediaLocator ml; if (fileSpecified == null) ml = new MediaLocator("vfw://0"); // VFW is for Windows. Use "sunvideoplus://0/1/JPEG" for Solaris else ml = new MediaLocator("file://" + fileSpecified); ds = Manager.createDataSource(ml); dataSource = Manager.createCloneableDataSource(ds); // Only open videoPanel once the vt's dataSource is set... if (!videoPanel.open(dataSource)) showError("Unable to play the data source."); else videoScrollPane.revalidate(); // Makes scroll pane show its contents } catch (Exception e) { e.printStackTrace(); if (fileSpecified == null) showError("Unable to connect to capture device.\nA compatible web cam must be installed "+ "for this application to stream video."); else showError("Unable to open file " + fileSpecified + " to display."); } } }} // End CamProcessor// Must be external class because it is instantiated using a public static method.class TransmitterThread extends Thread{ private String hostName; TransmitterThread(String hostName) { this.hostName = hostName; } public void run() { System.out.println("About to start VideoTransmitter..."); CamProcessor.vt = new VideoTransmitter( ((SourceCloneable)CamProcessor.dataSource).createClone(), hostName, "5050"); String result = CamProcessor.vt.start(); if (result != null) { System.err.println("Failed to start VideoTransmitter: " + result); return; } else System.out.println("Started VideoTransmitter."); CamProcessor.lblInfo.setText("Transmitting to host " + hostName + " on port 5050."); Thread thisThread = Thread.currentThread(); while (CamProcessor.transThread == thisThread && !CamProcessor.quitting) { try { thisThread.sleep(1000); } catch (InterruptedException e) {} } System.out.println("About to stop vt..."); CamProcessor.vt.stop(); // Stop transmission thread CAUSING PROBLEMS CamProcessor.lblInfo.setText(""); System.out.println("Stopped transThread"); }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -