Ask Your Question

i want to read image captured by webcam videocapture and write that to some external file

asked 2017-02-22 11:09:43 -0500

Rifat gravatar image

updated 2017-02-22 11:52:20 -0500

berak gravatar image

I am working on a project where my first step is to detect faces by webcam and the 2nd step is to save those images into some file .i am using opencv programs for facedetection.

imagetoshow contains the image of face detected by the webcam.The code line for this is as:

Image imageToShow = grabFrame();

i did first step but i am getting problem in second step Code snippet to read the image that i am using is below:

       f = new File("imageToShow"); //image file path
       image = new BufferedImage(width, height,
       image =;
       System.out.println("Reading complete.");
 }catch(IOException e){
       System.out.println("Error: "+e);

It seems that it is not reading anything from the file specified .Am i doing it in the right way?

edit retag flag offensive close merge delete


can you try again, and use the 10101 button, to format your code, not the " one ?

berak gravatar imageberak ( 2017-02-22 11:14:13 -0500 )edit

it's getting better, keep on trying ;)

berak gravatar imageberak ( 2017-02-22 11:35:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-02-22 11:33:48 -0500

berak gravatar image

you should use opencv functions, to save / load your image:

// write:
int counter = 0;
VideoCapture cap = new VideoCapture(0);
while(cap.isOpened()) {
     Mat frame = new Mat();
     if ( {
         Imgcodecs.write( "my."+counter+".png", frame); // Highgui, for outdated opencv2.4

// read:
Mat img = Imgcodecs.imread("my.17.png");
edit flag offensive delete link more


thankyou so much for your help.

Rifat gravatar imageRifat ( 2017-02-22 11:37:22 -0500 )edit

when i executed it separately ,it worked well. But i want to include it in a program where face is detected and saved as an object of Image class. The code is below:

Runnable frameGrabber = new Runnable()

                public void run()
                    Image imageToShow = grabFrame();
                    //System.out.println("inside runnable");
                        Platform.runLater(() -> {
                                catch(Exception ex)

                          Imgcodecs.imwrite( "my"+".png",imageToShow );


it gives the error message "The method imwrite(String, Mat) in the type Imgcodecs is not applicable for the arguments (String, Image)"

Rifat gravatar imageRifat ( 2017-02-24 08:03:10 -0500 )edit

what is the way to do it.?

Rifat gravatar imageRifat ( 2017-02-24 08:05:36 -0500 )edit

what is Image ?

berak gravatar imageberak ( 2017-02-24 09:04:39 -0500 )edit

it is a class which is imported by the program

import javafx.scene.image.Image;
Rifat gravatar imageRifat ( 2017-02-24 09:33:41 -0500 )edit

i downloaded it from the link [(]

Rifat gravatar imageRifat ( 2017-02-24 09:40:54 -0500 )edit

oh, that link was useful !

so, - you have to use Mat for the camera and detection things (also for imwrite() !), then later convert it to Image using mat2Image to display it with javafx.

maybe here is a good place to insert the imwrite()

berak gravatar imageberak ( 2017-02-24 09:53:28 -0500 )edit

Thanks a lot .You made it so simple.

Rifat gravatar imageRifat ( 2017-02-28 09:35:27 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-02-22 11:09:43 -0500

Seen: 787 times

Last updated: Feb 22 '17