SimpleMoviePlayer.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 package com.generalrobotix.ui.view.tdview;
00011 
00012 import java.awt.*;
00013 import java.awt.event.*;
00014 import java.awt.image.*;
00015 import java.io.*;
00016 import javax.swing.*;
00017 import javax.media.*;
00018 import javax.media.control.*;
00019 import javax.media.format.*;
00020 
00021 import com.generalrobotix.ui.util.ErrorDialog;
00022 import com.generalrobotix.ui.util.MessageBundle;
00023 import com.sun.image.codec.jpeg.*; 
00024 
00031 @SuppressWarnings("serial") //$NON-NLS-1$
00032 public class SimpleMoviePlayer extends JFrame implements ControllerListener {
00033     
00034     public boolean appFlag_=false;//単体アプリとして起動してるかflag
00035 
00036     private Component cmpVisual_=null;// 画面
00037     private Component cmpOpe_=null;// 操作板
00038     private Processor p_=null;// プロセッサ
00039     private FramePositioningControl frameCtrl_=null;// 位置コントロール
00040     private FrameGrabbingControl frameGrabCtrl_=null;// 画面いただきコントロール
00041     // 待ち用
00042     private Object waitSync = new Object();
00043     private  boolean stateTransitionOK = true;
00044     //表示用
00045     private final String STR_TITLE_="SMPlayer"; //ウィンドウタイトル文字列 //$NON-NLS-1$
00046     private final String STR_RIGHT_="(C) 2000 Kernel Inc"; //版権文字列(おまけ) //$NON-NLS-1$
00047 
00052     public SimpleMoviePlayer(){
00053         this(null,false);
00054     }
00055     
00056     public SimpleMoviePlayer(boolean appFlag){
00057         this(null,appFlag);
00058     }
00064     public SimpleMoviePlayer(String fileName){
00065         this(fileName,false);
00066     }
00067     public SimpleMoviePlayer(String fileName,boolean appFlag){
00068         super();
00069         
00070         appFlag_=appFlag;
00071         
00072         this.addWindowListener(
00073           new WindowAdapter() {
00074               public void windowClosing(WindowEvent evt) {
00075                   _quit();
00076               }
00077               public void windowClosed(WindowEvent evt) {
00078               }
00079           }
00080         );
00081 
00082         //メニュー
00083 
00084         Menu menu=new Menu(MessageBundle.get("SimpleMoviePlayer.menu.file")); //$NON-NLS-1$
00085         MenuBar bar=new MenuBar();
00086         bar.add(menu);
00087 
00088         MenuItem item;
00089 
00090         item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.open")); //$NON-NLS-1$
00091         item.addActionListener(new ActionListener(){
00092             public void actionPerformed(ActionEvent e){
00093                 _open();
00094             }            
00095         });
00096         menu.add(item);
00097 
00098         item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.saveAs")); //$NON-NLS-1$
00099         item.addActionListener(new ActionListener(){
00100             public void actionPerformed(ActionEvent e){
00101                 _saveImageAs();
00102             }            
00103         });
00104         menu.add(item);
00105 
00106         item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.quit")); //$NON-NLS-1$
00107         item.addActionListener(new ActionListener(){
00108             public void actionPerformed(ActionEvent e){
00109                 _quit();
00110             }            
00111         });
00112         menu.add(item);
00113         
00114         this.setMenuBar(bar);
00115 
00116 
00117         //指定ファイルオープン
00118         if(_load(fileName)==false)_load(null);
00119 
00120         //見栄えその他
00121         this.setVisible(true);
00122 
00123     }
00124 
00132     private MediaLocator _createMediaLocator(String url) {
00133 
00134         MediaLocator ml;
00135         if(url==null)return null;
00136 
00137         if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null) //$NON-NLS-1$
00138             return ml;
00139 
00140         if (url.startsWith(File.separator)) {
00141             if ((ml = new MediaLocator("file:" + url)) != null) //$NON-NLS-1$
00142             return ml;
00143         } else {
00144             String file = "file:" + System.getProperty("user.dir") + File.separator + url; //$NON-NLS-1$ //$NON-NLS-2$
00145             if ((ml = new MediaLocator(file)) != null)
00146             return ml;
00147         }
00148 
00149         return null;
00150     }
00151     
00156     private void _quit(){
00157         _remove();
00158         if(appFlag_){
00159             System.exit(0);
00160         }else{
00161             setVisible(false);
00162         }
00163     }
00164 
00165     
00170     private void _remove(){
00171         //remove
00172         if (cmpVisual_!=null){
00173             this.getContentPane().remove(cmpVisual_);
00174             cmpVisual_=null;
00175         }
00176         if (cmpOpe_!=null){
00177             this.getContentPane().remove(cmpOpe_);
00178             cmpOpe_=null;
00179         }
00180         
00181         if (p_!=null){
00182             p_.removeControllerListener(this);
00183             p_.stop();
00184             p_.close();
00185             p_=null;
00186         }
00187     }
00188 
00198     private boolean _load(String fileName) {
00199         Component newVisual=null;
00200         Component newOpe=null;
00201         
00202         _remove();
00203         
00204         //System.out.println(fileName);
00205         
00206         //fileName==nullならml=nullとなる
00207         MediaLocator ml=_createMediaLocator(fileName);
00208 
00209         //System.out.println(ml);
00210 
00211 
00212         if(ml==null){
00213             newVisual =new JPanel();//ダミー
00214             ((JPanel)newVisual).setPreferredSize(new Dimension(160,120));
00215             newVisual.setBackground(Color.black);
00216             newOpe=new JLabel(STR_RIGHT_);
00217             this.setTitle(STR_TITLE_);
00218         }else{
00219             this.setTitle(MessageBundle.get("SimpleMoviePlayer.title.openning") + ml +"..."); //$NON-NLS-1$ //$NON-NLS-2$
00220             try {
00221                 p_ = Manager.createProcessor(ml);
00222             } catch (Exception e) {
00223                 System.err.println("Failed to create a processor from the given url: " + e); //$NON-NLS-1$
00224                 return false;
00225             }
00226 
00227             p_.addControllerListener(this);
00228 
00229             // Put the Processor into configured state.
00230             p_.configure();
00231             if (!_waitForState(Processor.Configured)) {
00232                 System.err.println("Failed to configure the processor."); //$NON-NLS-1$
00233                 return false;
00234             }
00235 
00236             // So I can use it as a player.
00237             p_.setContentDescriptor(null);
00238 
00239             // Obtain the track controls.
00240             TrackControl tc[] = p_.getTrackControls();
00241 
00242             if (tc == null) {
00243                 System.err.println("Failed to obtain track controls from the processor."); //$NON-NLS-1$
00244                 return false;
00245             }
00246 
00247             // Search for the track control for the video track.
00248             TrackControl videoTrack = null;
00249 
00250             for (int i = 0; i < tc.length; i++) {
00251                 if (tc[i].getFormat() instanceof VideoFormat) {
00252                     videoTrack = tc[i];
00253                     break;
00254                 }
00255             }
00256 
00257             if (videoTrack == null) {
00258                 System.err.println("The input media does not contain a video track."); //$NON-NLS-1$
00259                 return false;
00260             }
00261 
00262             
00263             //FramePositioningControlを得る
00264             Object[] cnts;
00265             frameCtrl_=null;
00266             cnts=p_.getControls();
00267             for(int i=0;i<cnts.length;i++){
00268                 if(cnts[i] instanceof FramePositioningControl){
00269                     frameCtrl_=(FramePositioningControl)cnts[i];
00270                     //System.out.println(cnts[i]);
00271                 }
00272             }
00273 
00274             
00275 
00276             // Realize the processor.
00277             p_.prefetch();
00278             if (!_waitForState(Controller.Prefetched)) {
00279                 System.err.println("Failed to realize the processor."); //$NON-NLS-1$
00280                 return false;
00281             }
00282 
00283             //Rendererを得る
00284             javax.media.Renderer renderer=null;
00285             frameGrabCtrl_=null;
00286             cnts=videoTrack.getControls();
00287             for(int i=0;i<cnts.length;i++){
00288                 if(cnts[i] instanceof javax.media.Renderer){
00289                     renderer=(javax.media.Renderer)cnts[i];
00290                     //System.out.println(cnts[i]);
00291                 }
00292             }
00293             
00294             //FrameGrabbingControlを得る
00295             frameGrabCtrl_=null;
00296             cnts=renderer.getControls();
00297             for(int i=0;i<cnts.length;i++){
00298                 if(cnts[i] instanceof FrameGrabbingControl){
00299                     frameGrabCtrl_=(FrameGrabbingControl)cnts[i];
00300                     //System.out.println(cnts[i]);
00301                 }
00302             }
00303 
00304 
00305             // Display the visual & control component if there's one.
00306             newVisual = p_.getVisualComponent();
00307             
00308             JPanel panel=new JPanel();
00309             panel.setLayout(new BorderLayout());
00310             Component cc;
00311             if ((cc = p_.getControlPanelComponent()) != null) {
00312                 panel.add("Center", cc); //$NON-NLS-1$
00313             }
00314             JButton btn=new JButton(MessageBundle.get("SimpleMoviePlayer.button.save"));  //$NON-NLS-1$
00315             btn.addActionListener(new  ActionListener(){
00316                     public void actionPerformed(ActionEvent e){
00317                         _saveImageAs();
00318                     }
00319                 }
00320             );
00321             panel.add("East", btn); //$NON-NLS-1$
00322             newOpe=panel;
00323             
00324             this.setTitle(STR_TITLE_ + " -" + ml); //$NON-NLS-1$
00325         }
00326         
00327 
00328         //add
00329         this.getContentPane().setLayout(new BorderLayout());
00330         cmpVisual_=newVisual;
00331         if (cmpVisual_!= null) {
00332             getContentPane().add("Center", cmpVisual_); //$NON-NLS-1$
00333         }
00334         
00335         cmpOpe_=newOpe;
00336         if (cmpOpe_ != null) {
00337             getContentPane().add("South", cmpOpe_); //$NON-NLS-1$
00338         }
00339         this.pack();
00340 
00341         //はじめの画面を表示
00342         if(frameCtrl_!=null){
00343             frameCtrl_.skip(1);
00344             frameCtrl_.skip(-1);
00345          }
00346 
00347         return true;
00348     }
00349 
00354     private void _saveImageAs(){
00355         if (p_ == null) return;
00356         if (p_.getState()==Controller.Started)
00357             p_.stop();
00358         
00359         try{
00360             JFileChooser chooser =
00361                 new JFileChooser(System.getProperty("user.dir")); //$NON-NLS-1$
00362             ExampleFileFilter filter = new ExampleFileFilter();
00363             chooser.setDialogType(JFileChooser.SAVE_DIALOG);
00364             filter.addExtension("jpg"); //$NON-NLS-1$
00365             filter.setDescription("Jpeg Image"); //$NON-NLS-1$
00366             chooser.setFileFilter(filter);
00367             int returnVal = chooser.showSaveDialog(this);
00368             if(returnVal == JFileChooser.APPROVE_OPTION) {
00369                 FileOutputStream output =
00370                     new FileOutputStream(
00371                         chooser.getSelectedFile().getAbsolutePath()
00372                     );
00373                 JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(output);
00374                 enc.encode(_convertBufferedImage(frameGrabCtrl_.grabFrame() )); 
00375                 //enc.encode(codec_.getLastBufferedImage()); 
00376                 output.close();
00377             }
00378         }catch(Exception exception){
00379 /*
00380             JOptionPane.showMessageDialog(
00381                 this,
00382                 "Can't Save Image :" + exception.getMessage(),
00383                 "Error",
00384                 JOptionPane.ERROR_MESSAGE
00385             ); 
00386 */
00387             new ErrorDialog(
00388                 this,
00389                 MessageBundle.get("SimpleMoviePlayer.dialog.title.error"), //$NON-NLS-1$
00390                 MessageBundle.get("SimpleMoviePlayer.dialog.message.save") + exception.getMessage() //$NON-NLS-1$
00391             );
00392         }
00393     }
00394 
00399     private void _open(){
00400         JFileChooser chooser = new JFileChooser(System.getProperty("user.dir")); //$NON-NLS-1$
00401         // Note: source for ExampleFileFilter can be found in FileChooserDemo,
00402         // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
00403         ExampleFileFilter filter = new ExampleFileFilter();
00404         filter.addExtension("mpg"); //$NON-NLS-1$
00405         filter.addExtension("avi"); //$NON-NLS-1$
00406         filter.addExtension("mov"); //$NON-NLS-1$
00407         filter.setDescription("Movie Files"); //$NON-NLS-1$
00408         chooser.setFileFilter(filter);
00409         int returnVal = chooser.showOpenDialog(this);
00410         if(returnVal == JFileChooser.APPROVE_OPTION) {
00411             if(!_load("file:" + chooser.getSelectedFile().getAbsolutePath())){ //$NON-NLS-1$
00412                 //JOptionPane.showMessageDialog(this,  "Can't Open Movie.", "Error", JOptionPane.ERROR_MESSAGE); 
00413                 new ErrorDialog(
00414                     this,
00415                     MessageBundle.get("SimpleMoviePlayer.dialog.title.error"), //$NON-NLS-1$
00416                     MessageBundle.get("SimpleMoviePlayer.dialog.message.Open") //$NON-NLS-1$
00417                 );
00418                 _load(null);
00419             }
00420         }
00421         
00422     }
00423 
00431     private BufferedImage _convertBufferedImage(Buffer buf){
00432             MyBufferToImage bti =new MyBufferToImage((VideoFormat)buf.getFormat());
00433             Image img=bti.createImage(buf);
00434             if(img==null){
00435                 System.out.println("Can't create Image in this format!"); //$NON-NLS-1$
00436                 return null;
00437             }else{
00438                 BufferedImage bimg=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
00439                 Graphics2D g=bimg.createGraphics();
00440                 g.drawImage(img,0,0,null);
00441                 return bimg;
00442             }
00443     }
00444 
00453     private boolean _waitForState(int state) {
00454         synchronized (waitSync) {
00455             try {
00456                 while (p_.getState() != state && stateTransitionOK)
00457                     waitSync.wait();
00458             } catch (Exception e) {}
00459         }
00460         return stateTransitionOK;
00461     }
00462 
00463 
00468      public void controllerUpdate(ControllerEvent evt) {
00469 
00470         if (evt instanceof ConfigureCompleteEvent ||
00471             evt instanceof RealizeCompleteEvent ||
00472             evt instanceof PrefetchCompleteEvent) {
00473             synchronized (waitSync) {
00474                 stateTransitionOK = true;
00475                 waitSync.notifyAll();
00476             }
00477         } else if (evt instanceof ResourceUnavailableEvent) {
00478             synchronized (waitSync) {
00479                 stateTransitionOK = false;
00480                 waitSync.notifyAll();
00481             }
00482             
00483         }
00484     }
00485 
00486 
00487 
00492     public static void main(String [] args) {
00493 
00494         if (args.length == 0) {
00495             new SimpleMoviePlayer(true);
00496         }else{
00497             new SimpleMoviePlayer(args[0],true);
00498         }
00499 
00500     }
00501 
00502 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:19