Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

JMF screen capture

Thread view: 
Aki Laukkanen - 02 Nov 2005 13:34 GMT
Hey guys,

While trying to create a screenshot from a JMF MediaPlayer, I ran into
the following problem: If the Player has been started once, even if it's
 stopped later, any screenshots taken from it show just a black
rectangle.
I've tried both
"java.awt.Robot#createScreenCapture(Player.getVisualComponent().getBounds());"
and
"Player.getVisualComponent().paint(java.awt.image.BufferedImage#createGraphics());"
, but the only frame I've managed to capture is the one initially visible.
Has anyone ran into similiar problems with the JMF? Is there a solution
or a work-around for this?

Here's a program demonstrating the problem:

<SSCCE(?)>

import java.awt.Frame;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.imageio.ImageIO;
import java.io.File;
import javax.media.bean.playerbean.MediaPlayer;

public class VideoCaptureDemo {

  /**
   * Can probably be any MPEG or AVI video.
   * All files I used to test the program caused the same problem.
   * */
  private static final String VID_FILE = "file:H:mortalfighter.mpg";

  /**
   * Screenshots go to this dir.
   * */
  private static final String TARGET_DIR = "h:\\test\\";

  private Frame appFrame;
  private MediaPlayer vid = new MediaPlayer();
  private Button testButton;
  private Robot screenCapturer;
  private int capturesCreated;

  public VideoCaptureDemo() {
    capturesCreated = 0;
  }

  /**
   * Test harness
   * @param args not used
   * */

  public static void main(String[] args) {
    VideoCaptureDemo theDemo = new VideoCaptureDemo();
    theDemo.initGUI();
  }

  /**
   * Populate appFrame and set it visible.
   * */

  private void initGUI() {
    appFrame = new Frame("Pixel Grabbing Test");
    testButton = new Button("Press to test");
    testButton.addActionListener(new TestButtonListener());
    appFrame.setLayout(new BorderLayout());
    appFrame.addWindowListener(new TestFrameListener());
    appFrame.add(vid,BorderLayout.CENTER);
    appFrame.add(testButton, BorderLayout.SOUTH);
    appFrame.pack();
    appFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    vid.setVisible(true);
    appFrame.validate();
    appFrame.setVisible(true);
    vid.setControlPanelVisible(true);
    vid.setMediaLocation(VID_FILE);

    vid.prefetch();
    vid.setVisible(true);
    vid.setIgnoreRepaint(false);
    appFrame.repaint();
    try {
      screenCapturer = new Robot();
    }
    catch (AWTException ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }

  private class TestButtonListener implements ActionListener {
    /**
     * Simplified response to button click, validity checks etc.
     * omitted.
     *
     * @param e ActionEvent detected
     */
    public void actionPerformed(ActionEvent e) {

      try {
        if(screenCapturer == null){}
        else{
            boolean isPlaying;
            int vidMode = vid.getState();
            if(vidMode==vid.Started){
              isPlaying = true;
            }
            else{
              isPlaying = false;
            }
            vid.prefetch();
            vid.stop();
            vid.repaint();
            vid.getVisualComponent().repaint();
            BufferedImage screenImage =
screenCapturer.createScreenCapture(vid.getVisualComponent().getBounds());

            if(isPlaying){
              vid.start();
            }
            else{}
            ImageIO.write(screenImage, "jpeg",
new File(TARGET_DIR+"vid"+Integer.toString(capturesCreated)+".jpg"));

            capturesCreated++;
        }
      }
      catch (Exception exc) {
        exc.printStackTrace();
      }
    }
  }

  class TestFrameListener implements WindowListener {
    //Obligatory WindowListener boilerplate
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}

    /**
     * React to window closing
     * @param e WindowClosingEvent detected
     * */
    public void windowClosing(WindowEvent e) {
      System.exit( -1);
    }
  }
}

</SSCCE>

Signature

-Aki "Sus" Laukkanen

Andrew Thompson - 02 Nov 2005 13:53 GMT
> "java.awt.Robot#createScreenCapture(Player.getVisualComponent().getBounds());"

What are the value of the bounds being returned?

[ Personally I would recommend getting a screencapture of the
entire screen and worrying about the 'trivial details'* later ]

* WTF on this screen do I actually want again?   ;-)
Aki Laukkanen - 02 Nov 2005 14:34 GMT
>> "java.awt.Robot#createScreenCapture(Player.getVisualComponent().getBounds());"
>
> What are the value of the bounds being returned?

Appearently 0,0,893,705.

> [ Personally I would recommend getting a screencapture of the
> entire screen and worrying about the 'trivial details'* later ]
>
> * WTF on this screen do I actually want again?   ;-)

I tried this and got an image of the application GUI, but with a black
rectangle in place of the video image.

Signature

-Aki "Sus" Laukkanen

Roedy Green - 02 Nov 2005 14:45 GMT
On Wed, 02 Nov 2005 14:34:32 +0200, Aki Laukkanen
<aki.laukkanen@helsinki.fi> wrote, quoted or indirectly quoted someone
who said :

>While trying to create a screenshot from a JMF MediaPlayer, I ran into
>the following problem: If the Player has been started once, even if it's
>  stopped later, any screenshots taken from it show just a black
>rectangle.

the problem could be JMF is using some very low level technique to get
bits on the screen quickly and the Robot class can't see that low into
the bowels of the video hardware.

I suggest looking inside JMF for a means to capture a frame.

You might look for a NON-accelerated codec.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Aki Laukkanen - 02 Nov 2005 16:38 GMT
> On Wed, 02 Nov 2005 14:34:32 +0200, Aki Laukkanen
> <aki.laukkanen@helsinki.fi> wrote, quoted or indirectly quoted someone
[quoted text clipped - 8 lines]
> bits on the screen quickly and the Robot class can't see that low into
> the bowels of the video hardware.

Augh, just what I was afraid of. :( This complicates things a lot.

> I suggest looking inside JMF for a means to capture a frame.

Well, javax.media.control.FrameGrabbingControl seems to work to this end
for *some* formats...
<rant>
The selection of *fully* working JMF formats seems to be pretty narrow,
though.
setRate, FramePositioningControl and FrameGrabbingControl, for example,
only work on a scant few.
Hope they'll do something about that in the near future...
</rant>

Signature

-Aki "Sus" Laukkanen

Knute Johnson - 02 Nov 2005 21:24 GMT
>> On Wed, 02 Nov 2005 14:34:32 +0200, Aki Laukkanen
>> <aki.laukkanen@helsinki.fi> wrote, quoted or indirectly quoted someone
[quoted text clipped - 22 lines]
> Hope they'll do something about that in the near future...
> </rant>

FrameGrabbingControl appears to work well with Video for Windows and not
much else.  Somebody on another list the other day mentioned a problem
with MPEG video and FGC.

I doubt very much that you will see any further work done on JMF.  There
haven't been any updates in a couple of years.  I think it is dead.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 03 Nov 2005 05:37 GMT
On Wed, 02 Nov 2005 12:24:05 -0800, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>I doubt very much that you will see any further work done on JMF.  There
>haven't been any updates in a couple of years.  I think it is dead.

There was an update for JMF MP3 about a year ago.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 03 Nov 2005 05:26 GMT
On Wed, 02 Nov 2005 17:38:57 +0200, Aki Laukkanen
<aki.laukkanen@helsinki.fi> wrote, quoted or indirectly quoted someone
who said :

>> the problem could be JMF is using some very low level technique to get
>> bits on the screen quickly and the Robot class can't see that low into
>> the bowels of the video hardware.
>
>Augh, just what I was afraid of. :( This complicates things a lot.

the other thing to try is a full screen Robot grab. It might be
implemented a different way that will get you the raw bits.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.