简体   繁体   English

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

[英]Webcam doesn't work in java

It is not logic ? 这不是逻辑吗? because I added the webcam panel to the frame. 因为我将网络摄像头面板添加到框架中。 So it normally shows a view of my webcam inside the JFrame. 因此,它通常会在JFrame中显示我的网络摄像头的视图。 Anyone can help to to view the webcam really into the JFrame view ? 任何人都可以帮助您真正地在JFrame视图中查看网络摄像头吗? After my researches on google, I didn't find how to do it. 在Google上进行研究后,我没有找到方法。

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                   


}

The problem is with the auto-generated UI code. 问题在于自动生成的UI代码。 The WebcamPanel is added to the frame but the auto-generated code effectively makes the WebcamPanel invisible. WebcamPanel已添加到框架,但是自动生成的代码有效地使WebcamPanel不可见。 Try to use the WebcamPanel in a simpler UI and it will work; 尝试在更简单的UI中使用WebcamPanel ,它将起作用。 it worked fine for me. 对我来说很好。

Update for the edit: 更新以进行编辑:

The WebcamPanel is nowhere added to the content pane of the JFrame . WebcamPanel不会添加到JFrame的内容窗格中。 Add the WebcamPanel properly to the content pane of the JFrame and it will show. 将WebcamPanel正确地添加到JFrame的内容窗格中,它将显示出来。

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

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