Created
November 6, 2011 11:29
-
-
Save gustavohenrique/1342769 to your computer and use it in GitHub Desktop.
Example v4l4j
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.gustavohenrique; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.SwingUtilities; | |
import au.edu.jcu.v4l4j.CaptureCallback; | |
import au.edu.jcu.v4l4j.FrameGrabber; | |
import au.edu.jcu.v4l4j.V4L4JConstants; | |
import au.edu.jcu.v4l4j.VideoDevice; | |
import au.edu.jcu.v4l4j.VideoFrame; | |
import au.edu.jcu.v4l4j.exceptions.StateException; | |
import au.edu.jcu.v4l4j.exceptions.V4L4JException; | |
public class VideoCaptureApp extends WindowAdapter implements CaptureCallback { | |
private static int width = 640, height = 480, std = V4L4JConstants.STANDARD_WEBCAM, channel = 0; | |
private static String device = "/dev/video0"; | |
private VideoDevice videoDevice; | |
private FrameGrabber frameGrabber; | |
private JLabel label; | |
private JFrame frame; | |
public static void main(String args[]) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new VideoCaptureApp(); | |
} | |
}); | |
} | |
public VideoCaptureApp() { | |
try { | |
initFrameGrabber(); | |
} catch (V4L4JException e1) { | |
System.err.println("Error setting up capture"); | |
e1.printStackTrace(); | |
// cleanup and exit | |
cleanupCapture(); | |
return; | |
} | |
// create and initialise UI | |
initGUI(); | |
// start capture | |
try { | |
frameGrabber.startCapture(); | |
} | |
catch (V4L4JException e) { | |
System.err.println("Error starting the capture"); | |
e.printStackTrace(); | |
} | |
} | |
private void initFrameGrabber() throws V4L4JException { | |
videoDevice = new VideoDevice(device); | |
frameGrabber = videoDevice.getJPEGFrameGrabber(width, height, channel, std, 80); | |
frameGrabber.setCaptureCallback(this); | |
width = frameGrabber.getWidth(); | |
height = frameGrabber.getHeight(); | |
System.out.println("Starting capture at " + width + "x" + height); | |
} | |
private void initGUI() { | |
frame = new JFrame(); | |
label = new JLabel(); | |
JButton button = new JButton("Capture"); | |
button.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
} | |
}); | |
frame.getContentPane().add(label); | |
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
frame.addWindowListener(this); | |
frame.setVisible(true); | |
frame.setSize(width, height); | |
} | |
private void cleanupCapture() { | |
try { | |
frameGrabber.stopCapture(); | |
} | |
catch (StateException ex) { | |
// the frame grabber may be already stopped, so we just ignore | |
// any exception and simply continue. | |
} | |
// release the frame grabber and video device | |
videoDevice.releaseFrameGrabber(); | |
videoDevice.release(); | |
} | |
public void windowClosing(WindowEvent e) { | |
cleanupCapture(); | |
frame.dispose(); | |
System.out.println("exiting!!!"); | |
} | |
@Override | |
public void exceptionReceived(V4L4JException e) { | |
// This method is called by v4l4j if an exception | |
// occurs while waiting for a new frame to be ready. | |
// The exception is available through e.getCause() | |
e.printStackTrace(); | |
} | |
@Override | |
public void nextFrame(VideoFrame frame) { | |
// This method is called when a new frame is ready. | |
// Don't forget to recycle it when done dealing with the frame. | |
// draw the new frame onto the JLabel | |
label.getGraphics().drawImage(frame.getBufferedImage(), 0, 0, width, height, null); | |
// recycle the frame | |
frame.recycle(); | |
} | |
} | |
/* | |
download: | |
svn co http://v4l4j.googlecode.com/svn/v4l4j/trunk v4l4j | |
compiling: | |
ant clean all | |
sudo ant install | |
dependencies: | |
ant | |
make | |
gcc | |
libjpeg-dev | |
java-1.6.0-openjdk-devel | |
libgcj-devel | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment