SimpleMoviePlayer.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 package com.generalrobotix.ui.view.tdview;
11 
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.awt.image.*;
15 import java.io.*;
16 import javax.swing.*;
17 import javax.media.*;
18 import javax.media.control.*;
19 import javax.media.format.*;
20 
23 import com.sun.image.codec.jpeg.*;
24 
31 @SuppressWarnings("serial") //$NON-NLS-1$
32 public class SimpleMoviePlayer extends JFrame implements ControllerListener {
33 
34  public boolean appFlag_=false;//単体アプリとして起動してるかflag
35 
36  private Component cmpVisual_=null;// 画面
37  private Component cmpOpe_=null;// 操作板
38  private Processor p_=null;// プロセッサ
39  private FramePositioningControl frameCtrl_=null;// 位置コントロール
40  private FrameGrabbingControl frameGrabCtrl_=null;// 画面いただきコントロール
41  // 待ち用
42  private Object waitSync = new Object();
43  private boolean stateTransitionOK = true;
44  //表示用
45  private final String STR_TITLE_="SMPlayer"; //ウィンドウタイトル文字列 //$NON-NLS-1$
46  private final String STR_RIGHT_="(C) 2000 Kernel Inc"; //版権文字列(おまけ) //$NON-NLS-1$
47 
53  this(null,false);
54  }
55 
56  public SimpleMoviePlayer(boolean appFlag){
57  this(null,appFlag);
58  }
64  public SimpleMoviePlayer(String fileName){
65  this(fileName,false);
66  }
67  public SimpleMoviePlayer(String fileName,boolean appFlag){
68  super();
69 
70  appFlag_=appFlag;
71 
72  this.addWindowListener(
73  new WindowAdapter() {
74  public void windowClosing(WindowEvent evt) {
75  _quit();
76  }
77  public void windowClosed(WindowEvent evt) {
78  }
79  }
80  );
81 
82  //メニュー
83 
84  Menu menu=new Menu(MessageBundle.get("SimpleMoviePlayer.menu.file")); //$NON-NLS-1$
85  MenuBar bar=new MenuBar();
86  bar.add(menu);
87 
88  MenuItem item;
89 
90  item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.open")); //$NON-NLS-1$
91  item.addActionListener(new ActionListener(){
92  public void actionPerformed(ActionEvent e){
93  _open();
94  }
95  });
96  menu.add(item);
97 
98  item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.saveAs")); //$NON-NLS-1$
99  item.addActionListener(new ActionListener(){
100  public void actionPerformed(ActionEvent e){
101  _saveImageAs();
102  }
103  });
104  menu.add(item);
105 
106  item = new MenuItem(MessageBundle.get("SimpleMoviePlayer.menu.quit")); //$NON-NLS-1$
107  item.addActionListener(new ActionListener(){
108  public void actionPerformed(ActionEvent e){
109  _quit();
110  }
111  });
112  menu.add(item);
113 
114  this.setMenuBar(bar);
115 
116 
117  //指定ファイルオープン
118  if(_load(fileName)==false)_load(null);
119 
120  //見栄えその他
121  this.setVisible(true);
122 
123  }
124 
132  private MediaLocator _createMediaLocator(String url) {
133 
134  MediaLocator ml;
135  if(url==null)return null;
136 
137  if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null) //$NON-NLS-1$
138  return ml;
139 
140  if (url.startsWith(File.separator)) {
141  if ((ml = new MediaLocator("file:" + url)) != null) //$NON-NLS-1$
142  return ml;
143  } else {
144  String file = "file:" + System.getProperty("user.dir") + File.separator + url; //$NON-NLS-1$ //$NON-NLS-2$
145  if ((ml = new MediaLocator(file)) != null)
146  return ml;
147  }
148 
149  return null;
150  }
151 
156  private void _quit(){
157  _remove();
158  if(appFlag_){
159  System.exit(0);
160  }else{
161  setVisible(false);
162  }
163  }
164 
165 
170  private void _remove(){
171  //remove
172  if (cmpVisual_!=null){
173  this.getContentPane().remove(cmpVisual_);
174  cmpVisual_=null;
175  }
176  if (cmpOpe_!=null){
177  this.getContentPane().remove(cmpOpe_);
178  cmpOpe_=null;
179  }
180 
181  if (p_!=null){
182  p_.removeControllerListener(this);
183  p_.stop();
184  p_.close();
185  p_=null;
186  }
187  }
188 
198  private boolean _load(String fileName) {
199  Component newVisual=null;
200  Component newOpe=null;
201 
202  _remove();
203 
204  //System.out.println(fileName);
205 
206  //fileName==nullならml=nullとなる
207  MediaLocator ml=_createMediaLocator(fileName);
208 
209  //System.out.println(ml);
210 
211 
212  if(ml==null){
213  newVisual =new JPanel();//ダミー
214  ((JPanel)newVisual).setPreferredSize(new Dimension(160,120));
215  newVisual.setBackground(Color.black);
216  newOpe=new JLabel(STR_RIGHT_);
217  this.setTitle(STR_TITLE_);
218  }else{
219  this.setTitle(MessageBundle.get("SimpleMoviePlayer.title.openning") + ml +"..."); //$NON-NLS-1$ //$NON-NLS-2$
220  try {
221  p_ = Manager.createProcessor(ml);
222  } catch (Exception e) {
223  System.err.println("Failed to create a processor from the given url: " + e); //$NON-NLS-1$
224  return false;
225  }
226 
227  p_.addControllerListener(this);
228 
229  // Put the Processor into configured state.
230  p_.configure();
231  if (!_waitForState(Processor.Configured)) {
232  System.err.println("Failed to configure the processor."); //$NON-NLS-1$
233  return false;
234  }
235 
236  // So I can use it as a player.
237  p_.setContentDescriptor(null);
238 
239  // Obtain the track controls.
240  TrackControl tc[] = p_.getTrackControls();
241 
242  if (tc == null) {
243  System.err.println("Failed to obtain track controls from the processor."); //$NON-NLS-1$
244  return false;
245  }
246 
247  // Search for the track control for the video track.
248  TrackControl videoTrack = null;
249 
250  for (int i = 0; i < tc.length; i++) {
251  if (tc[i].getFormat() instanceof VideoFormat) {
252  videoTrack = tc[i];
253  break;
254  }
255  }
256 
257  if (videoTrack == null) {
258  System.err.println("The input media does not contain a video track."); //$NON-NLS-1$
259  return false;
260  }
261 
262 
263  //FramePositioningControlを得る
264  Object[] cnts;
265  frameCtrl_=null;
266  cnts=p_.getControls();
267  for(int i=0;i<cnts.length;i++){
268  if(cnts[i] instanceof FramePositioningControl){
269  frameCtrl_=(FramePositioningControl)cnts[i];
270  //System.out.println(cnts[i]);
271  }
272  }
273 
274 
275 
276  // Realize the processor.
277  p_.prefetch();
278  if (!_waitForState(Controller.Prefetched)) {
279  System.err.println("Failed to realize the processor."); //$NON-NLS-1$
280  return false;
281  }
282 
283  //Rendererを得る
284  javax.media.Renderer renderer=null;
285  frameGrabCtrl_=null;
286  cnts=videoTrack.getControls();
287  for(int i=0;i<cnts.length;i++){
288  if(cnts[i] instanceof javax.media.Renderer){
289  renderer=(javax.media.Renderer)cnts[i];
290  //System.out.println(cnts[i]);
291  }
292  }
293 
294  //FrameGrabbingControlを得る
295  frameGrabCtrl_=null;
296  cnts=renderer.getControls();
297  for(int i=0;i<cnts.length;i++){
298  if(cnts[i] instanceof FrameGrabbingControl){
299  frameGrabCtrl_=(FrameGrabbingControl)cnts[i];
300  //System.out.println(cnts[i]);
301  }
302  }
303 
304 
305  // Display the visual & control component if there's one.
306  newVisual = p_.getVisualComponent();
307 
308  JPanel panel=new JPanel();
309  panel.setLayout(new BorderLayout());
310  Component cc;
311  if ((cc = p_.getControlPanelComponent()) != null) {
312  panel.add("Center", cc); //$NON-NLS-1$
313  }
314  JButton btn=new JButton(MessageBundle.get("SimpleMoviePlayer.button.save")); //$NON-NLS-1$
315  btn.addActionListener(new ActionListener(){
316  public void actionPerformed(ActionEvent e){
317  _saveImageAs();
318  }
319  }
320  );
321  panel.add("East", btn); //$NON-NLS-1$
322  newOpe=panel;
323 
324  this.setTitle(STR_TITLE_ + " -" + ml); //$NON-NLS-1$
325  }
326 
327 
328  //add
329  this.getContentPane().setLayout(new BorderLayout());
330  cmpVisual_=newVisual;
331  if (cmpVisual_!= null) {
332  getContentPane().add("Center", cmpVisual_); //$NON-NLS-1$
333  }
334 
335  cmpOpe_=newOpe;
336  if (cmpOpe_ != null) {
337  getContentPane().add("South", cmpOpe_); //$NON-NLS-1$
338  }
339  this.pack();
340 
341  //はじめの画面を表示
342  if(frameCtrl_!=null){
343  frameCtrl_.skip(1);
344  frameCtrl_.skip(-1);
345  }
346 
347  return true;
348  }
349 
354  private void _saveImageAs(){
355  if (p_ == null) return;
356  if (p_.getState()==Controller.Started)
357  p_.stop();
358 
359  try{
360  JFileChooser chooser =
361  new JFileChooser(System.getProperty("user.dir")); //$NON-NLS-1$
362  ExampleFileFilter filter = new ExampleFileFilter();
363  chooser.setDialogType(JFileChooser.SAVE_DIALOG);
364  filter.addExtension("jpg"); //$NON-NLS-1$
365  filter.setDescription("Jpeg Image"); //$NON-NLS-1$
366  chooser.setFileFilter(filter);
367  int returnVal = chooser.showSaveDialog(this);
368  if(returnVal == JFileChooser.APPROVE_OPTION) {
369  FileOutputStream output =
370  new FileOutputStream(
371  chooser.getSelectedFile().getAbsolutePath()
372  );
373  JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(output);
374  enc.encode(_convertBufferedImage(frameGrabCtrl_.grabFrame() ));
375  //enc.encode(codec_.getLastBufferedImage());
376  output.close();
377  }
378  }catch(Exception exception){
379 /*
380  JOptionPane.showMessageDialog(
381  this,
382  "Can't Save Image :" + exception.getMessage(),
383  "Error",
384  JOptionPane.ERROR_MESSAGE
385  );
386 */
387  new ErrorDialog(
388  this,
389  MessageBundle.get("SimpleMoviePlayer.dialog.title.error"), //$NON-NLS-1$
390  MessageBundle.get("SimpleMoviePlayer.dialog.message.save") + exception.getMessage() //$NON-NLS-1$
391  );
392  }
393  }
394 
399  private void _open(){
400  JFileChooser chooser = new JFileChooser(System.getProperty("user.dir")); //$NON-NLS-1$
401  // Note: source for ExampleFileFilter can be found in FileChooserDemo,
402  // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
403  ExampleFileFilter filter = new ExampleFileFilter();
404  filter.addExtension("mpg"); //$NON-NLS-1$
405  filter.addExtension("avi"); //$NON-NLS-1$
406  filter.addExtension("mov"); //$NON-NLS-1$
407  filter.setDescription("Movie Files"); //$NON-NLS-1$
408  chooser.setFileFilter(filter);
409  int returnVal = chooser.showOpenDialog(this);
410  if(returnVal == JFileChooser.APPROVE_OPTION) {
411  if(!_load("file:" + chooser.getSelectedFile().getAbsolutePath())){ //$NON-NLS-1$
412  //JOptionPane.showMessageDialog(this, "Can't Open Movie.", "Error", JOptionPane.ERROR_MESSAGE);
413  new ErrorDialog(
414  this,
415  MessageBundle.get("SimpleMoviePlayer.dialog.title.error"), //$NON-NLS-1$
416  MessageBundle.get("SimpleMoviePlayer.dialog.message.Open") //$NON-NLS-1$
417  );
418  _load(null);
419  }
420  }
421 
422  }
423 
431  private BufferedImage _convertBufferedImage(Buffer buf){
432  MyBufferToImage bti =new MyBufferToImage((VideoFormat)buf.getFormat());
433  Image img=bti.createImage(buf);
434  if(img==null){
435  System.out.println("Can't create Image in this format!"); //$NON-NLS-1$
436  return null;
437  }else{
438  BufferedImage bimg=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
439  Graphics2D g=bimg.createGraphics();
440  g.drawImage(img,0,0,null);
441  return bimg;
442  }
443  }
444 
453  private boolean _waitForState(int state) {
454  synchronized (waitSync) {
455  try {
456  while (p_.getState() != state && stateTransitionOK)
457  waitSync.wait();
458  } catch (Exception e) {}
459  }
460  return stateTransitionOK;
461  }
462 
463 
468  public void controllerUpdate(ControllerEvent evt) {
469 
470  if (evt instanceof ConfigureCompleteEvent ||
471  evt instanceof RealizeCompleteEvent ||
472  evt instanceof PrefetchCompleteEvent) {
473  synchronized (waitSync) {
474  stateTransitionOK = true;
475  waitSync.notifyAll();
476  }
477  } else if (evt instanceof ResourceUnavailableEvent) {
478  synchronized (waitSync) {
479  stateTransitionOK = false;
480  waitSync.notifyAll();
481  }
482 
483  }
484  }
485 
486 
487 
492  public static void main(String [] args) {
493 
494  if (args.length == 0) {
495  new SimpleMoviePlayer(true);
496  }else{
497  new SimpleMoviePlayer(args[0],true);
498  }
499 
500  }
501 
502 }
static final String get(String key)
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_uint_32 i
Definition: png.h:2735
png_bytep buf
Definition: png.h:2729
SimpleMoviePlayer(String fileName, boolean appFlag)
output(gif_dest_ptr dinfo, int code)
Definition: wrgif.c:105


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:41