I cannot seem to capture a image from webcam and save it to a file located on my desktop..
webcam displays fine and face detection works fine.

just having trouble with capturing and saving the image from recording.
tried several options that are commented out. (some were deleted completely)



main class
import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.PrintWriter;


import javax.swing.JFrame;

import javax.imageio.ImageIO;


import java.io.*;


import org.bytedeco.javacpp.opencv_core.IplImage;

import org.bytedeco.javacv.OpenCVFrameGrabber;

import org.bytedeco.javacv.FrameGrabber.Exception;

import org.opencv.core.*;

import org.opencv.highgui.*;

import org.opencv.objdetect.CascadeClassifier;

public class CVDemo {
public static void main(String[] args) throws IOException, InterruptedException, Exception {
System.loadLibrary("opencv_java2410");
String window_name = "Face detection";
JFrame frame = new JFrame(window_name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
processor my_processor=new processor(); // class attached in post

My_Panel my_panel = new My_Panel(); //class attacked in post

frame.setContentPane(my_panel);
frame.setVisible(true);
//-- 2. Read the video stream

Mat webcam_image=new Mat();
VideoCapture capture =new VideoCapture(1); // webcam

// Mat image = new Mat(); //

// BufferedImage img= null;

// OpenCVFrameGrabber grabber=new OpenCVFrameGrabber(1);

// CascadeClassifier face_cascade = new CascadeClassifier("C:/opencv/sources/data/haarcascades/haarcascade_profileface.xml");

// MatOfByte mem = new MatOfByte();


if( capture.isOpened())
{
// img=ImageIO.read(image);

Thread.sleep(1000); //delay for webcam to load

// capture.read(image);

// Highgui.imwrite("C://Users//Robert//Desktop//camera.jpeg", image);


while(true){
if( !webcam_image.empty() )
{

frame.setSize(2*webcam_image.width()+40,2*webcam_image.height()+60);
//-- 3. Apply the classifier to the captured image

webcam_image=my_processor.detect(webcam_image);

//-- 4. Display the image

capture.retrieve(webcam_image);
// Thread.sleep(1000);

// Highgui.imwrite("C:/Users/Robert/Desktop/camera.jpg", webcam_image);


// Thread.sleep(1000);

my_panel.MatToBufferedImage(webcam_image); // We could look at the error...


my_panel.repaint();
// capture.read(image);

// Highgui.imwrite("camera.jpeg", image);



//OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);


}












}
}










return;
}
}



 my_panel class/ processor class

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferByte; 
import javax.swing.*; 
import org.opencv.core.Core; 
import org.opencv.core.Mat; 
import org.opencv.core.MatOfRect; 
import org.opencv.core.Point; 
import org.opencv.core.Rect; 
import org.opencv.core.Scalar; 
import org.opencv.core.Size; 
import org.opencv.highgui.VideoCapture; 
import org.opencv.imgproc.Imgproc; 
import org.opencv.objdetect.CascadeClassifier; 
class My_Panel extends JPanel{ 
    private static final long serialVersionUID = 1L; 
    private BufferedImage image; 
    // Create a constructor method 

    public My_Panel(){ 
        super();  
    } 
    /** 
     * Converts/writes a Mat into a BufferedImage. 
     *  
     * @param matrix Mat of type CV_8UC3 or CV_8UC1 
     * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY 
     */ 
    public boolean MatToBufferedImage(Mat matBGR){ 
      
        int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels() ; 
        byte[] sourcePixels = new byte[width * height * channels]; 
        matBGR.get(0, 0, sourcePixels); 
        // create new image and get reference to backing data 
        image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
        System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); 
       
       
        return true; 
    } 
    public void paintComponent(Graphics g){ 
        super.paintComponent(g);  
        if (this.image==null) return; 
        g.drawImage(this.image,10,10,2*this.image.getWidth(),2*this.image.getHeight(), null); 
        //g.drawString("This is my custom Panel!",10,20); 
    } 


class processor { 
    private CascadeClassifier face_cascade;
   
    // Create a constructor method 
    public processor(){ 
        face_cascade=new CascadeClassifier("C:/opencv/sources/data/haarcascades/haarcascade_profileface.xml");
      
        if(face_cascade.empty()) 
        { 
            System.out.println("--(!)Erroring A\n"); 
            return; 
        } 
        else 
        { 
            System.out.println("Begin"); 
        } 
    } 
    public Mat detect(Mat inputframe){   
        long startTime = System.nanoTime(); 
        Mat mRgba=new Mat(); 
        Mat mGrey=new Mat(); 
        MatOfRect faces = new MatOfRect();
        inputframe.copyTo(mRgba); 
        inputframe.copyTo(mGrey); 
        Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY); 
        Imgproc.equalizeHist( mGrey, mGrey ); 
        face_cascade.detectMultiScale(mGrey, faces); 
        long endTime = System.nanoTime(); 
        for(Rect rect:faces.toArray()) 
        { 
            Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 ); 
            Core.ellipse( mRgba, center, new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 1, 0, 1 ), 4, 8, 0 ); 
        }
        return mRgba; 
    }
}