簡體   English   中英

網絡攝像頭在Java中不起作用

[英]Webcam doesn't work in java

這不是邏輯嗎? 因為我將網絡攝像頭面板添加到框架中。 因此,它通常會在JFrame中顯示我的網絡攝像頭的視圖。 任何人都可以幫助您真正地在JFrame視圖中查看網絡攝像頭嗎? 在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                   


}

問題在於自動生成的UI代碼。 WebcamPanel已添加到框架,但是自動生成的代碼有效地使WebcamPanel不可見。 嘗試在更簡單的UI中使用WebcamPanel ,它將起作用。 對我來說很好。

更新以進行編輯:

WebcamPanel不會添加到JFrame的內容窗格中。 將WebcamPanel正確地添加到JFrame的內容窗格中,它將顯示出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM