这不是逻辑吗?因为我将网络摄像头面板添加到了框架中。所以它通常会在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
}
答案 0 :(得分:0)
问题在于自动生成的UI代码。 WebcamPanel
已添加到框架中,但自动生成的代码有效地使WebcamPanel
不可见。尝试在更简单的UI中使用WebcamPanel
,它将起作用;它对我来说很好。
WebcamPanel无法添加到JFrame
的内容窗格中。将WebcamPanel正确添加到JFrame
的内容窗格中,它将显示。