简体   繁体   中英

Adding an image obtained from webcam to an existing JPanel

I was trying to add an image I capture from a webcam to an existing JPanel when a button is clicked, but the JPanel never displays the image. Can anyone point me in the right direction please?

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

    try {
        grabber.start();
        IplImage img = grabber.grab();
        if (img != null) {

            img1 = img.getBufferedImage();
            ImageIcon icon = new ImageIcon(img1);
            label = new JLabel(icon);
            //photo is the name of the JPanel
            photo.add(label);
            photo.setVisible(true);
            grabber.stop();
            System.out.println("done");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

EDIT: This is the whole class. (Trimmed it to make it easier to read)

package honoursproject;

    import com.googlecode.javacv.OpenCVFrameGrabber;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


public class AddPerson extends javax.swing.JFrame {

/** Creates new form AddPerson */
public AddPerson() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    nameField = new javax.swing.JTextField();
    surnameField = new javax.swing.JTextField();
    photo = new javax.swing.JPanel();
    captureButton = new javax.swing.JButton();
    saveButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    photo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0,       0)));

    pack();
}// </editor-fold>

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    JFrame frame = new JFrame();
    try {
        grabber.start();
        IplImage img = grabber.grab();

        if (img != null) {

       img1 = img.getBufferedImage();
        ImageIcon icon = new ImageIcon(img1);
        label = new JLabel(icon);
        //photo is the name of the JPanel
        photo.add(label);
        photo.setVisible(true);
        grabber.stop();
        System.out.println("done");


            grabber.stop();
            System.out.println("done");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}                                             



public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new AddPerson().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JButton captureButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField nameField;
private javax.swing.JPanel photo;
private javax.swing.JButton saveButton;
private javax.swing.JTextField surnameField;
// End of variables declaration

}

If you're adding a new component to your photo JPanel you will need to update & paint the container:

photo.revalidate();
photo.repaint();

Alternatively, the JLabel could also be added at startup, allowing you to call setIcon to update the image.

Update:

You don't appear to add your JPanel photo to your JFrame anywhere:

add(photo);

Don't create another JFrame to display your image. Add it to your JPanel using the original JFrame . See this post .

我通过使用JLabel而不是JFrame或JPanel解决了该问题,并且可以正常工作。

您忘记了调用setIcon

label.setIcon(icon);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM