Re: [javacv] Problem with EventKey - JavaCV

176 views
Skip to first unread message

Samuel Audet

unread,
Feb 23, 2013, 9:40:16 PM2/23/13
to jav...@googlegroups.com
Hello,

On 02/21/2013 08:35 AM, Michele Correa wrote:
> Hi! I am new here... =], And I need some help:
>
> I am trying to write a program that displays a video using JavaCV and I
> want that the window (or the video) closes when ESC is pressed. My code:

CanvasFrame.waitKey() wasn't meant to be called in the EDT thread. Try
to run it in the main thread, i.e.:

public static void main(String[] args) {
CanvasFrame canvas = new CanvasFrame("title)";
while (...) {
if (canvas.waitKey(2).getKeyChar() == VK_ESCAPE) {
break;
}
}
}

Samuel

Michele Correa

unread,
Feb 24, 2013, 4:32:46 PM2/24/13
to jav...@googlegroups.com
Hi, thanks for your help ! I tried your suggestion, but when I don't call the CanvasFrame.waitKey() in the EDT thread, the code doesn't compile and "Eclipse" suggests that I call it in the EDT thread or add throws declaration and then the video isn't displayed, =\

Have I done something wrong?
Message has been deleted

Samuel Audet

unread,
Mar 3, 2013, 3:23:58 AM3/3/13
to jav...@googlegroups.com
Hello,

On 02/25/2013 06:33 AM, Michele Correa wrote:
> Hi, thanks for your help ! I tried your suggestion, but when I don't
> call the CanvasFrame.waitKey() in the EDT thread, the code doesn't
> compile and "Eclipse" suggests that I call it in the EDT thread or add
> throws declaration and then the video isn't displayed, =\
>
> Have I done something wrong?

I guess so, I'm not getting any issues doing something like this in the
while() loop of Demo.java:
KeyEvent e = frame.waitKey(2);
if (e != null && e.getKeyChar() == KeyEvent.VK_ESCAPE) {
break;
}
But this probably doesn't do exactly what you're after.. I'll be
updating it shortly so that we can call waitKey(-1), which probably does
something closer to what you're looking for.

Samuel

Michele Correa

unread,
Mar 10, 2013, 6:32:46 PM3/10/13
to jav...@googlegroups.com
Hi, thanks again, =]! Now the video is playing, but it doesn't stop when I press ESC. My code:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class VideoJavaCV extends JFrame {  

public static void main (String[] args){
JFrame window = new JFrame("JavaCV");
final JDesktopPane desktop = new JDesktopPane();   
        JMenuBar myMenuBar = new JMenuBar();
        JMenu myMenuFile = new JMenu("File");
        JMenuItem openVideo = new JMenuItem("Open Video");
        myMenuBar.add(myMenuFile);
        myMenuFile.add(openVideo);
        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        System.exit(0);
        }
        });
        
        myMenuFile.add(exit);
        
        openVideo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            String path = "";
            JFileChooser file = new JFileChooser();
            if(file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) path = file.getSelectedFile().getAbsolutePath();
                final CanvasFrame canvas = new CanvasFrame("Video Canvas"); 
                
                canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);   
                FrameGrabber grabber = new OpenCVFrameGrabber(path);
                canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight());
                try {      
                grabber.start();
                IplImage img;
               
                while(true){
                img = grabber.grab();
                if(img!=null){
                canvas.showImage(img);
                KeyEvent er = canvas.waitKey(2); 
                        if (er != null && er.getKeyChar() == KeyEvent.VK_ESCAPE) { 
                        return; 
                        }
                }
                }
                }catch (Exception ae) {}

            }
            
        });
        
        
        
        window.add(desktop);
        window.setJMenuBar(myMenuBar);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(200, 100);
        window.setVisible(true);
}
}   

Samuel Audet

unread,
Mar 11, 2013, 5:18:12 AM3/11/13
to jav...@googlegroups.com
On 03/11/2013 07:32 AM, Michele Correa wrote:
> Hi, thanks again, =]! Now the video is playing, but it doesn't stop when
> I press ESC. My code:

Please try to add the following lines to the end of the while() loop of
the Demo class found in the README.txt file:
KeyEvent e = frame.waitKey(-1);
if (e != null && e.getKeyChar() == KeyEvent.VK_ESCAPE) {
break;
}

Samuel

Michele Correa

unread,
Mar 24, 2013, 5:54:32 PM3/24/13
to jav...@googlegroups.com
Hi! I've tried this too, but it didn't work.

I noticed that when I remove all the buttons and menus (JMenuBar, JMenu, JMenuItem etc) everything works perfectly, =]. Is there any incompatibility between them and JavaCV / OpenCV?

Thank you,
Michele.

Samuel Audet

unread,
Mar 24, 2013, 11:15:21 PM3/24/13
to jav...@googlegroups.com
On 03/25/2013 06:54 AM, Michele Correa wrote:
> I noticed that when I remove all the buttons and menus (JMenuBar, JMenu,
> JMenuItem etc) everything works perfectly, =]. Is there any
> incompatibility between them and JavaCV / OpenCV?

No, this is just Java. If you're going to add menu bars and stuff you're
probably better off using a KeyListener to do what you want to do:
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Reply all
Reply to author
Forward
0 new messages