SWTMoviePlayer.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.view.tdview;
2 
3 import java.io.*;
4 import java.lang.reflect.InvocationTargetException;
5 
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.awt.Component;
9 import java.awt.Dimension;
10 import java.awt.Frame;
11 import java.awt.Graphics2D;
12 import java.awt.Image;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.image.BufferedImage;
16 
17 import javax.media.Buffer;
18 import javax.media.ConfigureCompleteEvent;
19 import javax.media.Controller;
20 import javax.media.ControllerEvent;
21 import javax.media.ControllerListener;
22 import javax.media.Manager;
23 import javax.media.MediaLocator;
24 import javax.media.PrefetchCompleteEvent;
25 import javax.media.Processor;
26 import javax.media.RealizeCompleteEvent;
27 import javax.media.ResourceUnavailableEvent;
28 import javax.media.control.FrameGrabbingControl;
29 import javax.media.control.FramePositioningControl;
30 import javax.media.control.TrackControl;
31 import javax.media.format.VideoFormat;
32 import javax.swing.JButton;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.SwingUtilities;
36 
38 import com.sun.image.codec.jpeg.*;
39 
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.awt.SWT_AWT;
43 import org.eclipse.swt.events.ControlEvent;
44 import org.eclipse.swt.events.ControlListener;
45 import org.eclipse.swt.events.SelectionAdapter;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.ShellEvent;
48 import org.eclipse.swt.events.ShellListener;
49 import org.eclipse.swt.layout.FillLayout;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Display;
52 import org.eclipse.swt.widgets.FileDialog;
53 import org.eclipse.swt.widgets.Menu;
54 import org.eclipse.swt.widgets.MenuItem;
55 import org.eclipse.swt.widgets.Shell;
56 
57 public class SWTMoviePlayer implements ControllerListener{
58  private Component cmpVisual_=null;// 画面
59  private Component cmpOpe_=null;// 操作板
60 
61  private Processor p_=null;// プロセッサ
62  private FramePositioningControl frameCtrl_=null;// 位置コントロール
63  private FrameGrabbingControl frameGrabCtrl_=null;// 画面いただきコントロール
64  // 待ち用
65  private Object waitSync = new Object();
66  private boolean stateTransitionOK = true;
67  //表示用
68  private final String STR_TITLE_="SMPlayer"; //ウィンドウタイトル文字列 //$NON-NLS-1$
69  private final String STR_RIGHT_="(C) 2000 Kernel Inc"; //版権文字列(おまけ) //$NON-NLS-1$
70 
71  Shell window_ = null;
72  Frame frame_;
73  Composite comp_;
74  JPanel contentPane_;
75  int frameX,frameY;
76 
82  public SWTMoviePlayer( Shell shell, String fileName ){
83 
84  window_ = new Shell( shell, SWT.SHELL_TRIM );
85  window_.setSize(200, 200);
86  window_.setText(MessageBundle.get("SWTMoviePlayer.window.title")); //$NON-NLS-1$
87 
88  createMenu();
89 
90  FillLayout layout = new FillLayout();
91  window_.setLayout(layout);
92 
93  //----
94  // Linuxでリサイズイベントが発行されない問題対策
95  // https://bugs.eclipse.org/bugs/show_bug.cgi?id=168330
96  comp_ = new Composite( window_, SWT.EMBEDDED );
97  frame_ = SWT_AWT.new_Frame( comp_ );
98 
99  comp_.addControlListener( new ControlListener() {
100  public void controlMoved(ControlEvent e) {}
101  public void controlResized(ControlEvent e) {
102  frame_.setBounds(0, 0, comp_.getSize().x, comp_.getSize().y );
103  }
104  });
105  //----
106 
107  window_.addShellListener(new ShellListener(){
108  public void shellActivated(ShellEvent e) {
109  // TODO 自動生成されたメソッド・スタブ
110  }
111  public void shellClosed(ShellEvent e) {
112  // TODO 自動生成されたメソッド・スタブ
113  _remove();
114  }
115  public void shellDeactivated(ShellEvent e) {
116  // TODO 自動生成されたメソッド・スタブ
117 
118  }
119  public void shellDeiconified(ShellEvent e) {
120  // TODO 自動生成されたメソッド・スタブ
121 
122  }
123  public void shellIconified(ShellEvent e) {
124  // TODO 自動生成されたメソッド・スタブ
125 
126  }
127  });
128 
129  contentPane_ = new JPanel();
130  frame_.add(contentPane_);
131 
132  window_.open();
133  window_.setSize(contentPane_.getPreferredSize().width, contentPane_.getPreferredSize().height);
134 
135  //枠線サイズを取得する
136  frameX = window_.getSize().x - comp_.getSize().x;
137  frameY = window_.getSize().y - comp_.getSize().y;
138  System.out.println("frame size="+frameX+"-"+frameY); //$NON-NLS-1$ //$NON-NLS-2$
139 
140  //指定ファイルオープン
141  if(_load(fileName)==false)_load(null);
142  }
143 
144  private void resizeWindow(){
145  Display display = Display.getDefault();
146 
147  if ( display!=null && !display.isDisposed())
148  // TODO: syncExecではこけるのだがなぜ?
149  display.asyncExec(
150  new Runnable(){
151  public void run() {
152  window_.setSize(contentPane_.getPreferredSize().width+frameX, contentPane_.getPreferredSize().height+frameY);
153  }
154  }
155  );
156 
157  }
158 
159  private void createMenu() {
160  Menu menubar = new Menu(window_,SWT.BAR);
161  window_.setMenuBar(menubar);
162 
163  MenuItem item1 = new MenuItem(menubar,SWT.CASCADE);
164  item1.setText(MessageBundle.get("SWTMoviePlayer.menu.file")); //$NON-NLS-1$
165 
166  Menu menu1 = new Menu(item1);
167  item1.setMenu(menu1);
168 
169  MenuItem item1_1 = new MenuItem(menu1,SWT.PUSH);
170  item1_1.setText(MessageBundle.get("SWTMoviePlayer.menu.open")); //$NON-NLS-1$
171  item1_1.addSelectionListener(new SelectionAdapter(){
172  public void widgetSelected(SelectionEvent e) {
173  _open();
174  }
175  });
176 
177  MenuItem item1_3 = new MenuItem(menu1,SWT.PUSH);
178  item1_3.setText(MessageBundle.get("SWTMoviePlayer.menu.saveAs")); //$NON-NLS-1$
179  item1_3.addSelectionListener(new SelectionAdapter(){
180  public void widgetSelected(SelectionEvent e) {
181  _saveImageAs();
182  }
183  });
184 
185  MenuItem item1_4 = new MenuItem(menu1,SWT.PUSH);
186  item1_4.setText(MessageBundle.get("SWTMoviePlayer.menu.quit")); //$NON-NLS-1$
187  item1_4.addSelectionListener(new SelectionAdapter(){
188  public void widgetSelected(SelectionEvent e) {
189  window_.close();
190  }
191  });
192 
193  }
194 
195 
200  File currentFile;
201  private void _open() {
202  FileDialog fDialog = new FileDialog(window_,SWT.OPEN);
203 
204  String [] exts = {"*.mpg;*.avi;*.mov"}; //$NON-NLS-1$
205  String [] filterNames = {MessageBundle.get("SWTMoviePlayer.dialog.filter")}; //$NON-NLS-1$
206  fDialog.setFilterExtensions(exts);
207  fDialog.setFilterNames(filterNames);
208 
209  String openPath = fDialog.open();
210  if( openPath != null ) {
211  currentFile = new File( openPath );
212  /*
213  if(!_load("file:" + f.getAbsolutePath())){
214  MessageDialog.openError(window_, "Error", "Can't Open Movie." );
215  _load(null);
216  }
217  */
218  try {
219  SwingUtilities.invokeAndWait(new Runnable(){
220  public void run(){
221  if(!_load("file:" + currentFile.getAbsolutePath())){ //$NON-NLS-1$
222  _load(null);
223  }
224  }
225  });
226  } catch (InterruptedException e1) {
227  // TODO Auto-generated catch block
228  e1.printStackTrace();
229  } catch (InvocationTargetException e1) {
230  // TODO Auto-generated catch block
231  e1.printStackTrace();
232  }
233 
234  }
235 
236  /*
237  JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));
238  // Note: source for ExampleFileFilter can be found in FileChooserDemo,
239  // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
240  ExampleFileFilter filter = new ExampleFileFilter();
241  filter.addExtension("mpg");
242  filter.addExtension("avi");
243  filter.addExtension("mov");
244  filter.setDescription("Movie Files");
245  chooser.setFileFilter(filter);
246  int returnVal = chooser.showOpenDialog(frame_);
247  if(returnVal == JFileChooser.APPROVE_OPTION) {
248  if(!_load("file:" + chooser.getSelectedFile().getAbsolutePath())){
249  //JOptionPane.showMessageDialog(frame_, "Can't Open Movie.", "Error", JOptionPane.ERROR_MESSAGE);
250  new ErrorDialog(
251  this,
252  "Error",
253  "Can't Open Movie."
254  );
255  _load(null);
256  }
257  }
258  */
259  }
260 
265  private void _remove(){
266  //remove
267  if (cmpVisual_!=null){
268  contentPane_.remove(cmpVisual_);
269  cmpVisual_=null;
270  }
271  if (cmpOpe_!=null){
272  contentPane_.remove(cmpOpe_);
273  cmpOpe_=null;
274  }
275 
276  if (p_!=null){
277  p_.removeControllerListener(this);
278  p_.stop();
279  p_.close();
280  p_=null;
281  }
282  }
283 
291  private MediaLocator _createMediaLocator(String url) {
292 
293  MediaLocator ml;
294  if(url==null)return null;
295 
296  if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null) //$NON-NLS-1$
297  return ml;
298 
299  if (url.startsWith(File.separator)) {
300  if ((ml = new MediaLocator("file:" + url)) != null) //$NON-NLS-1$
301  return ml;
302  } else {
303  String file = "file:" + System.getProperty("user.dir") + File.separator + url; //$NON-NLS-1$ //$NON-NLS-2$
304  if ((ml = new MediaLocator(file)) != null)
305  return ml;
306  }
307 
308  return null;
309  }
310 
311 
312 
322  private boolean _load(String fileName) {
323  Component newVisual=null;
324  Component newOpe=null;
325 
326  _remove();
327 
328  System.out.println("filename is "+fileName); //$NON-NLS-1$
329 
330  //fileName==nullならml=nullとなる
331  MediaLocator ml=_createMediaLocator(fileName);
332 
333  System.out.println("ML="+ml); //$NON-NLS-1$
334 
335 
336  if(ml==null){
337  newVisual =new JPanel();//ダミー
338  ((JPanel)newVisual).setPreferredSize(new Dimension(160,120));
339  newVisual.setBackground(Color.black);
340  newOpe=new JLabel(STR_RIGHT_);
341  //window_.setText(STR_TITLE_);
342  }else{
343  //window_.setText("Openning " + ml +"...");
344  try {
345  p_ = Manager.createProcessor(ml);
346  } catch (Exception e) {
347  System.err.println("Failed to create a processor from the given url: " + e); //$NON-NLS-1$
348  return false;
349  }
350 
351  p_.addControllerListener(this);
352 
353  // Put the Processor into configured state.
354  p_.configure();
355  if (!_waitForState(Processor.Configured)) {
356  System.err.println("Failed to configure the processor."); //$NON-NLS-1$
357  return false;
358  }
359 
360  // So I can use it as a player.
361  p_.setContentDescriptor(null);
362 
363  // Obtain the track controls.
364  TrackControl tc[] = p_.getTrackControls();
365 
366  if (tc == null) {
367  System.err.println("Failed to obtain track controls from the processor."); //$NON-NLS-1$
368  return false;
369  }
370 
371  // Search for the track control for the video track.
372  TrackControl videoTrack = null;
373 
374  for (int i = 0; i < tc.length; i++) {
375  if (tc[i].getFormat() instanceof VideoFormat) {
376  videoTrack = tc[i];
377  break;
378  }
379  }
380 
381  if (videoTrack == null) {
382  System.err.println("The input media does not contain a video track."); //$NON-NLS-1$
383  return false;
384  }
385 
386 
387  //FramePositioningControlを得る
388  Object[] cnts;
389  frameCtrl_=null;
390  cnts=p_.getControls();
391  for(int i=0;i<cnts.length;i++){
392  if(cnts[i] instanceof FramePositioningControl){
393  frameCtrl_=(FramePositioningControl)cnts[i];
394  //System.out.println(cnts[i]);
395  }
396  }
397 
398 
399 
400  // Realize the processor.
401  p_.prefetch();
402  if (!_waitForState(Controller.Prefetched)) {
403  System.err.println("Failed to realize the processor."); //$NON-NLS-1$
404  return false;
405  }
406 
407  //Rendererを得る
408  javax.media.Renderer renderer=null;
409  frameGrabCtrl_=null;
410  cnts=videoTrack.getControls();
411  for(int i=0;i<cnts.length;i++){
412  if(cnts[i] instanceof javax.media.Renderer){
413  renderer=(javax.media.Renderer)cnts[i];
414  //System.out.println(cnts[i]);
415  }
416  }
417 
418  //FrameGrabbingControlを得る
419  frameGrabCtrl_=null;
420  cnts=renderer.getControls();
421  for(int i=0;i<cnts.length;i++){
422  if(cnts[i] instanceof FrameGrabbingControl){
423  frameGrabCtrl_=(FrameGrabbingControl)cnts[i];
424  //System.out.println(cnts[i]);
425  }
426  }
427 
428  // Display the visual & control component if there's one.
429  newVisual = p_.getVisualComponent();
430 
431  JPanel panel=new JPanel();
432  panel.setLayout(new BorderLayout());
433  Component cc;
434  if ((cc = p_.getControlPanelComponent()) != null) {
435  panel.add("Center", cc); //$NON-NLS-1$
436  }
437  JButton btn=new JButton(MessageBundle.get("SWTMoviePlayer.button.save")); //$NON-NLS-1$
438  btn.addActionListener(new ActionListener(){
439  public void actionPerformed(ActionEvent e){
440  _saveImageAs();
441  }
442  }
443  );
444  panel.add("East", btn); //$NON-NLS-1$
445  newOpe=panel;
446 
447  //window_.setText(STR_TITLE_ + " -" + ml);
448  }
449 
450 
451  //add
452  contentPane_.setLayout(new BorderLayout());
453  cmpVisual_=newVisual;
454  if (cmpVisual_!= null) {
455  contentPane_.add("Center", cmpVisual_); //$NON-NLS-1$
456  }
457 
458  cmpOpe_=newOpe;
459  if (cmpOpe_ != null) {
460  contentPane_.add("South", cmpOpe_); //$NON-NLS-1$
461  }
462 
463  //はじめの画面を表示
464  if(frameCtrl_!=null){
465  frameCtrl_.skip(1);
466  frameCtrl_.skip(-1);
467  }
468 
469  resizeWindow();
470 
471  return true;
472  }
473 
481  private BufferedImage _convertBufferedImage(Buffer buf){
482  MyBufferToImage bti =new MyBufferToImage((VideoFormat)buf.getFormat());
483  Image img=bti.createImage(buf);
484  if(img==null){
485  System.out.println("Can't create Image in this format!"); //$NON-NLS-1$
486  return null;
487  }else{
488  BufferedImage bimg=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
489  Graphics2D g=bimg.createGraphics();
490  g.drawImage(img,0,0,null);
491  return bimg;
492  }
493  }
494 
499  JPEGImageEncoder enc;
500  private void _saveImageAs(){
501  if (p_ == null) {
502  System.out.println("not init."); //$NON-NLS-1$
503  return;
504  }
505  try {
506  SwingUtilities.invokeAndWait(new Runnable(){
507  public void run(){
508  if (p_.getState()==Controller.Started)
509  p_.stop();
510  }
511  });
512  } catch (Exception e) {
513  e.printStackTrace();
514  }
515  try{
516  FileDialog fDialog = new FileDialog(window_,SWT.SAVE);
517 
518  String [] exts = {"*.jpg"}; //$NON-NLS-1$
519  String [] filterNames = {MessageBundle.get("SWTMoviePlayer.dialog.filter.jpeg")}; //$NON-NLS-1$
520  fDialog.setFilterExtensions(exts);
521  fDialog.setFilterNames(filterNames);
522 
523  String openPath = fDialog.open();
524  if( openPath != null ) {
525  File f = new File( openPath );
526 
527  FileOutputStream output =
528  new FileOutputStream(
529  f.getAbsolutePath()
530  );
531  enc=JPEGCodec.createJPEGEncoder(output);
532 
533  try {
534  SwingUtilities.invokeAndWait(new Runnable(){
535  public void run(){
536  try {
537  enc.encode(_convertBufferedImage(frameGrabCtrl_.grabFrame() ));
538  } catch (Exception e) {
539  // TODO Auto-generated catch block
540  e.printStackTrace();
541  }
542  }
543  });
544  } catch (Exception e) {
545  e.printStackTrace();
546  }
547 
548  //enc.encode(codec_.getLastBufferedImage());
549  output.close();
550  }
551 
552  /*
553  JFileChooser chooser =
554  new JFileChooser(System.getProperty("user.dir"));
555  ExampleFileFilter filter = new ExampleFileFilter();
556  chooser.setDialogType(JFileChooser.SAVE_DIALOG);
557  filter.addExtension("jpg");
558  filter.setDescription("Jpeg Image");
559  chooser.setFileFilter(filter);
560 
561  int returnVal = chooser.showSaveDialog(this);
562  if(returnVal == JFileChooser.APPROVE_OPTION) {
563  FileOutputStream output =
564  new FileOutputStream(
565  chooser.getSelectedFile().getAbsolutePath()
566  );
567  JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(output);
568  enc.encode(_convertBufferedImage(frameGrabCtrl_.grabFrame() ));
569  //enc.encode(codec_.getLastBufferedImage());
570  output.close();
571  }
572  */
573 
574  }catch(Exception exception){
575  /*
576  JOptionPane.showMessageDialog(
577  this,
578  "Can't Save Image :" + exception.getMessage(),
579  "Error",
580  JOptionPane.ERROR_MESSAGE
581  );
582  */
583  MessageDialog.openError(window_, MessageBundle.get("SWTMoviePlayer.dialog.title.error"), MessageBundle.get("SWTMoviePlayer.dialog.message.save") + exception.getMessage() ); //$NON-NLS-1$ //$NON-NLS-2$
584  }
585  }
586 
587 
596  private boolean _waitForState(int state) {
597  synchronized (waitSync) {
598  try {
599  while (p_.getState() != state && stateTransitionOK)
600  waitSync.wait();
601  } catch (Exception e) {}
602  }
603  return stateTransitionOK;
604  }
605 
606 
611  public void controllerUpdate(ControllerEvent evt) {
612  if (evt instanceof ConfigureCompleteEvent ||
613  evt instanceof RealizeCompleteEvent ||
614  evt instanceof PrefetchCompleteEvent) {
615  synchronized (waitSync) {
616  stateTransitionOK = true;
617  waitSync.notifyAll();
618  }
619  } else if (evt instanceof ResourceUnavailableEvent) {
620  synchronized (waitSync) {
621  stateTransitionOK = false;
622  waitSync.notifyAll();
623  }
624 
625  }
626  }
627 
628 }
static final String get(String key)
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_uint_32 i
Definition: png.h:2735
def run(tree, args)
png_bytep buf
Definition: png.h:2729
org
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