繁体   English   中英

从网络摄像头获取的图像添加到现有的JPanel

[英]Adding an image obtained from webcam to an existing JPanel

单击按钮时,我试图将从网络摄像机捕获的图像添加到现有的JPanel中,但是JPanel永远不会显示该图像。 谁能指出我正确的方向?

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();
    }

编辑:这是全班。 (对其进行了整理以使其更易于阅读)

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

}

如果要向photo JPanel添加新组件,则需要更新和绘制容器:

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

或者,也可以在启动时添加JLabel ,从而允许您调用setIcon来更新映像。

更新:

您似乎没有 JPanel photo 添加JFrame任何位置:

add(photo);

不要创建另一个JFrame来显示图像。 使用原始JFrame将其添加到您的JPanel 看到这篇文章

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

您忘记了调用setIcon

label.setIcon(icon);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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