思路
1、想要实现相机功能,就想要实现截图功能,但是eclipes里面没有调用电脑摄像头的jar包,所以需要在GitHub上面找(连接:https://github.com/sarxos/webcam-capture),下载并解压。
2、若要调用jar包,只需右键你的项目->properties->下图箭头所示
选择刚刚在GitHub上解压好的文件夹中的jar包,最后点击Apply and Close即可。
代码:
public class Camera extends JFrame{
public void initUI(){
this.setSize(800,600);
this.setTitle("相机");
this.setDefaultCloseOperation(3);
this.setVisible(true);
Graphics g = this.getGraphics();
Webcam webcam = Webcam.getDefault();
webcam.open();
//用while循环不断获取照片
while (true){
BufferedImage bi = webcam.getImage();
//将获取的照片 画到界面上
g.drawImage(bi, 100, 100, 400, 300,null);
}
}
public static void main(String[]args) {
Camera Cam = new Camera();
Cam.initUI();
}
}
运行结果:
结果如图,其实还可以添加一些其他的效果,但是我还在研究QAQ,敬请期待。