how to display the grabbed video frame inside JFrame

asked 2015-03-25 03:02:23 -0600

RB gravatar image

updated 2015-03-26 03:59:02 -0600

thdrksdfthmn gravatar image

I have posted a question in SO website and it is [here].

what i want to do is, to open a video of .avi extension, and in a while-loop i am reading the video fram by frame using the methods grab() and retrieve() as shown below in the code, and for each grabbed frame i am converting it to an image and display that image inside a JFrame.

given the code below, each grabbed frame is converted into an image correctly, i knew that it is converted into an imagr correctly because after the program finish execution i checked where the converted fram into image is located and it contains the last frame in the video. the problem is, the displayed image on the JFrame is not changing, despite the video contains many frames only the first frame is shown inside EVERY created JFrame, and what i want to do is, for every grabbed frame it should be displayed inside a new JFrame.

please have alook at the code posted below, and let me know why despite every grabbed frame is converted into image and saved to a specific path, when i display the saved image inside JFrame, only the first grabbed image is displayed inside the JFrame.

code:

public class PlayVideo {
   static VideoCapture vCapture;
   static Mat mat;

   public static void main(String[] args) {
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

      vCapture = new VideoCapture("C:/private/..../AVIs/ChuteLibre.avi");
      vCapture.open("C:/private/..../AVIs/ChuteLibre.avi");
      mat = new Mat();

      if (!vCapture.isOpened()) {
          System.out.println("media failed to open");
      } else {            
          while (vCapture.grab()) {
              vCapture.retrieve(mat);
              showInFrame(mat);
          }
          vCapture.release();
      }
   }

    private static void showInFrame(Mat mat) {
      // TODO Auto-generated method stub
      JFrame mediaFrame = new JFrame("Media");
      mediaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mediaFrame.setVisible(true);
      mediaFrame.setSize(300,300);

      Highgui.imwrite("c:/private/img.jpg", mat);
      ImageIcon image = new ImageIcon("c:/private/img.jpg");
      JLabel label = new JLabel("", image, JLabel.CENTER);

      mediaFrame.add(label);
      mediaFrame.repaint();
      mediaFrame.validate();
      mediaFrame.setVisible(true);
   }
}

(http://stackoverflow.com/questions/29...)

edit retag flag offensive close merge delete

Comments

Why would you like to display images in different windows? Have you seen this?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-25 08:04:34 -0600 )edit

for each grabbed or retrieved frame of the .avi file, i want to display it inside a new JFrame. if i used only one JFrame the all the retrieved frame from the .avi file will be displayed and accumulated inside one JFrame and in the end i would see only the last retrieved frame inside the JFrame

RB gravatar imageRB ( 2015-03-25 10:52:12 -0600 )edit

If frame is Mat, isn't JFrame displaying the mat? it is accumulating them inside? Strange... I do not know java very well, but I think that the imShow from the git repo is solving the problem of accumulating the frames... You want to do some differences on different frames? Then you should use a mat that keeps the previous frame and display the previous and current frames in different windows

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-25 11:24:04 -0600 )edit
1

i would like to clarify further, lets assume that that the metho grab() will retrieve 10 frames(f1,f2,f3...f10), as u see in the code, i save each frame as image and thens display it in the jframe..what happens at run time is, every generated jframe dispalys the f1 only while the lasted saved frame as image in the provided path is f10...AFAIK, this means that only the first retrieved frame f1 is displayed in jframe while the laset save frame as image in the path is f10...i dont know why this happening..i want to display each retrieved frame(f1,f2...f10) in a new jframe..i wish this clear

RB gravatar imageRB ( 2015-03-25 13:48:34 -0600 )edit

Then your problem is here: vCapture.retrieve(mat); showInFrame(mat); if your grab is getting 10 frames then your receive should add them all in your mat. If you want to display the each frame,in separate window, you shall split the mat in X images and display each one in a different window...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-26 03:56:21 -0600 )edit