java调用摄像头拍照,使用webcam-capture替换jmf调用摄像头拍照

java项目中由于jmf调用的时候在eclipse里面可以使用,导出jar文件的时候,会调不出来摄像头拍照,所以才用webcam-capture技术写一个方法,直接调用方法Photograph就可以实现拍照保存

public class Capture {
	private static JFrame window;

	public static void main(String[] args) {
		final Webcam webcam = Webcam.getDefault();
		window = new JFrame("摄像头");
		WebcamPanel panel = new WebcamPanel(webcam);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.add(panel, BorderLayout.CENTER);
		window.setResizable(true);
		window.pack();
		window.setVisible(true);
		Capture c = new Capture();
		c.Photograph(webcam);
		
	}
	public void Photograph(final Webcam webcam) {
		
		window.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosed(WindowEvent e) {
				webcam.close();
				window.dispose();
			}
		});
		
		String fileName = "C:/Users/Administrator/Pictures/pz/" + System.currentTimeMillis();
		WebcamUtils.capture(webcam, fileName,ImageUtils.FORMAT_PNG);
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				return;
			}
		});
	}	

}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Java调用摄像头进行拍照的示例代码: ```java import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import com.github.sarxos.webcam.Webcam; public class WebcamCaptureExample extends JPanel implements Runnable { private static final long serialVersionUID = 1L; private Webcam webcam = null; private BufferedImage image = null; public WebcamCaptureExample() { super(); setPreferredSize(new Dimension(640, 480)); webcam = Webcam.getDefault(); webcam.setViewSize(new Dimension(640, 480)); } @Override public void run() { while (true) { try { image = webcam.getImage(); repaint(); Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override protected void paintComponent(java.awt.Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage(image, 0, 0, getWidth(), getHeight(), this); } } public void start() { Thread t = new Thread(this); t.setDaemon(true); t.start(); } public void stop() { webcam.close(); } public static void main(String[] args) throws IOException { WebcamCaptureExample example = new WebcamCaptureExample(); JFrame window = new JFrame("Webcam Capture Example"); window.add(example); window.pack(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); example.start(); // 保存图片到文件 BufferedImage image = example.image; File file = new File("webcam_capture.png"); ImageIO.write(image, "png", file); example.stop(); } } ``` 该示例程序使用了 [Webcam Capture API](https://github.com/sarxos/webcam-capture) 来获取摄像头图像,需要引入对应的库文件。 程序的主要流程如下: 1. 获取默认摄像头对象并设置摄像头视野大小为640x480。 2. 实现Runnable接口,启动一个线程来获取摄像头图像并通过repaint()方法刷新面板。 3. 实现paintComponent()方法,在面板上绘制摄像头图像。 4. 启动线程,并在主线程中保存最后一帧摄像头图像到文件。 5. 在程序结束时关闭摄像头。 注意:在使用该示例代码之前,请先确保电脑上已经安装了摄像头并且能够正常使用
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值