Search code examples
javamysqlnetbeanswebcam

Take picture using webcam in netbeans java?


I have been busy with trying to get the webcam to work within netbeans over the past few days. The problem I am having is to get the coding to activate and take a picture using the webcam.

So far I have seen that I will have to use the OpenCV and some other JAR files. Please could someone help me out by perfecting the coding I have below:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // coding for webcam and taking a picture
    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    try
    {
        //start of the webcam for taking the picture
        grabber.start();
        Image IM = this.takePicture();
        //stops the webcam            
        grabber.stop();      


    }
    catch (Exception e)
    {
        //displays error message if problem with webcam
        JOptionPane.showMessageDialog(null, "Problem accessing or using the Webcam!");
    }

} 

What I need is for the picture to be displayed in a label on my interface after it has taken the picture.

The Open CV has been successfully installed and now just the coding needed to get the picture taken.

Any Help would be helpful.


Solution

  • Okay, your coding is a bit off, it does need some changes made to it. You are on the right track however.

    I do have a sample code that may be able to help you out which works:

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            // coding for webcam and taking a picture
            OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
            try
            {
                //start of the webcam for taking the picture
                grabber.start();
                //grabs teh image taken from the webcam
                IplImage img = grabber.grab();            
                //checks if the webcam has taken the picture and if the picture if mot empty
                if(img != null)
                {
                    //determines where to save the picture
                    cvSaveImage("C:\\User1\\PrifilePicture\\"+lbl_StudnetLogin.getText()+".jpeg", img);                         
                }
                //stops the webcam            
                grabber.stop();
                //used to resize teh picture taken in order to display picture to the user      
                Image imeResize = ImageIO.read(new File("C:\\SalVentri\\PrifilePicture\\"+lbl_StudnetLogin.getText()+".jpeg"));
                //1st ---> width _______2sn ---> height
                lbl_Profile.setIcon(new ImageIcon(imeResize.getScaledInstance(155, 100, 100)));
    
            }
            catch (Exception e)
            {
                //displays error message if problem with webcam
                JOptionPane.showMessageDialog(null, "Problem accessing or using the Webcam!");
            }
    
        } 
    

    Hope this helps