JAVA 调用摄像头

首先调用摄像头,需要 Java 驱动包

链接:https://pan.baidu.com/s/1pTr1jeVAubqvch33cjtq3Q 密码:paub

一 . 使用 代码编辑工具 创建一个Java Project

二. 找到该项目的工作空间目录 ,创建 lib目录

三. 返回代码编辑工具将 jar包添加的项目中来:

四.添加完成后,创建 一个class文件,将以下代码放入其中:

	public static void main(String[] args) throws InterruptedException {

		Webcam webcam = Webcam.getDefault();

		webcam.setViewSize(WebcamResolution.VGA.getSize());

		WebcamPanel panel = new WebcamPanel(webcam);

		panel.setFPSDisplayed(true);

		panel.setDisplayDebugInfo(true);

		panel.setImageSizeDisplayed(true);

		panel.setMirrored(true);

		JFrame window = new JFrame("Test webcam panel");

		window.add(panel);

		window.setResizable(true);

		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		window.pack();

		window.setVisible(true);

	}

运行main方法 即可弹出 :

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值