【问题标题】:Adding an image obtained from webcam to an existing JPanel将从网络摄像头获取的图像添加到现有的 JPanel
【发布时间】:2013-02-24 12:32:22
【问题描述】:

当单击按钮时,我试图将我从网络摄像头捕获的图像添加到现有的 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

}

【问题讨论】:

  • 有什么例外吗?您是否已经打印了图标等以查看它们的价值?
  • 无异常,程序完成无错误,并打印出“完成”。
  • 您使用的是布局管理器吗?您需要一次添加一张以上的图片吗?
  • 我正在使用布局管理器,但不,我只需要单击按钮添加一张图片。
  • 如需尽快获得更好的帮助,请发帖 SSCCE。为此,请排除所有网络摄像头的复杂性,只需使用new BufferedImage(40,40,BufferedImage.TYPE_INT_RGB) 在代码中生成图像。请参阅显示图像的 SSCCE 的this example

标签: java swing opencv webcam javacv


【解决方案1】:

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

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

另外,JLabel 也可以在启动时添加,允许您调用setIcon 来更新图像。

更新:

您似乎没有在任何地方添加您的JPanel photo 到您的JFrame

add(photo);

不要创建另一个JFrame 来显示您的图像。使用原始的JFrame 将其添加到您的JPanel。见this post

【讨论】:

  • 你打电话给revalidate了吗?
  • @davy307 图片可能不包含数据 JPanel photo 没有大小。考虑发布SSCCE
  • @mKorbel,我也尝试了 label.setIcon 并且它不起作用,除了 label = new JLabel(icon) 不做同样的事情吗?
  • @Reimeus,图像确实包含数据,这在我使用 JFrame 时非常有效,但是由于我试图创建的 GUI 类型,我需要嵌入 JFrame 中的 JPanel 中的图像.
  • 听起来像是布局管理器的问题,没看到框架代码就忍不住了。
【解决方案2】:

我通过使用 JLabel 而不是 JFrame 或 JPanel 解决了这个问题,它可以工作。

【讨论】:

    【解决方案3】:

    你忘了打电话给setIcon

    label.setIcon(icon);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      相关资源
      最近更新 更多