Question

I finally got JavaCV working with my webcam but I can't find a way to change the output format of the image grabbed.

I have a HP HD (720p) fixed webcam in my HP notebook. The only resolution I get is 640x480. I tried using all available subclasses of FrameGrabber.

I am using this simple code:

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class Webcam {

    public static void main(String[] args) throws Exception {
        FrameGrabber grabber = FrameGrabber.createDefault(0);
        grabber.start();

        IplImage grabbedImage = grabber.grab();

        CanvasFrame frame = new CanvasFrame("Some Title", CanvasFrame.getDefaultGamma() / grabber.getGamma());

        while (frame.isVisible() && (grabbedImage = grabber.grab()) != null) {
            frame.showImage(grabbedImage);
        }

        frame.dispose();
        grabber.stop();
    }

}

Am I missing something or is there another way to capture image from a webcam using JavaCV?

Was it helpful?

Solution

I found another way to capture from webcam and also set some properties like preferred width and height.

This code works for me:

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_highgui;
import com.googlecode.javacv.cpp.opencv_highgui.CvCapture;

public class Webcam {

    public static void main(String[] args) throws Exception {
        CvCapture capture = opencv_highgui.cvCreateCameraCapture(0);

        opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
        opencv_highgui.cvSetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);

        IplImage grabbedImage = opencv_highgui.cvQueryFrame(capture);

        CanvasFrame frame = new CanvasFrame("Webcam");

        while (frame.isVisible() && (grabbedImage = opencv_highgui.cvQueryFrame(capture)) != null) {
            frame.showImage(grabbedImage);
        }

        frame.dispose();
        opencv_highgui.cvReleaseCapture(capture);
    }

}

Hope this helps somebody with same problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top