Skip to content

Instantly share code, notes, and snippets.

@nikhil9
Created November 7, 2012 16:02
Show Gist options
  • Save nikhil9/4032453 to your computer and use it in GitHub Desktop.
Save nikhil9/4032453 to your computer and use it in GitHub Desktop.
Camera Access using Javacv
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.OpenCVFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
public class demo_video {
public static void main(String[] args) {
//Create canvas frame for displaying webcam.
CanvasFrame canvas = new CanvasFrame("Webcam");
//Set Canvas frame to close on exit
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//Declare FrameGrabber to import output from webcam
FrameGrabber grabber = new OpenCVFrameGrabber("");
try {
//Start grabber to capture video
grabber.start();
//Declare img as IplImage
IplImage img;
while (true) {
//inser grabed video fram to IplImage img
img = grabber.grab();
//Set canvas size as per dimentions of video frame.
canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight());
if (img != null) {
//Flip image horizontally
cvFlip(img, img, 1);
//Show video frame in canvas
canvas.showImage(img);
}
}
}
catch (Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment