网络摄像头在java中不起作用

时间:2016-02-07 14:00:50

标签: java

这不是逻辑吗?因为我将网络摄像头面板添加到了框架中。所以它通常会在JFrame中显示我的网络摄像头的视图。 任何人都可以帮助在JFrame视图中查看网络摄像头吗? 在我研究谷歌之后,我没有找到如何做到这一点。

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author matr
 */
public class NewJFrame extends javax.swing.JFrame {

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


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            // TODO add your handling code here:
            Webcam webcam = Webcam.getDefault();
            webcam.open();
            ImageIO.write(webcam.getImage(), "PNG", new File("hello-world.png"));
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    private void formComponentShown(java.awt.event.ComponentEvent evt) {                                    
        // TODO add your handling code here:
        Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.VGA.getSize());
        WebcamPanel panel = new WebcamPanel(webcam);
        panel.setFPSDisplayed(true);
        panel.setDisplayDebugInfo(true);
        panel.setImageSizeDisplayed(true);
        panel.setVisible(true);
        this.add(panel);
    }                                   



    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }

        });

    }



    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   


}

1 个答案:

答案 0 :(得分:0)

问题在于自动生成的UI代码。 WebcamPanel已添加到框架中,但自动生成的代码有效地使WebcamPanel不可见。尝试在更简单的UI中使用WebcamPanel,它将起作用;它对我来说很好。

编辑更新:

WebcamPanel无法添加到JFrame的内容窗格中。将WebcamPanel正确添加到JFrame的内容窗格中,它将显示。