Java 调用摄像头

                                         Java 调用摄像头

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开发工具与关键技术:servlet+jsp+jdbc

作者:LYG

撰写时间:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

从网上搜索出来的调用摄像头都很模糊,然后根据自己想要的功能摸索出来了调用摄像头的功能:

关键代码:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		String fun=request.getParameter("fun");
		//保存摄像头拍下的图片
		 String basePath = "upload/img/";
		    String filePath = request.getSession().getServletContext().getRealPath("/") + basePath;
		   // String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".png";
		    String fileName = (new Date()).getTime()+".png";
		    //默认传入的参数带类型等参数:data:image/png;base64,
		    String imgStr = request.getParameter("image");
		    if (null != imgStr) {
		        imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
		    }
		    Boolean flag = GenerateImage(imgStr, filePath, fileName);
		    String result = "";
		    if (flag) {
		        result = basePath + fileName;
		    }
		    response.getWriter().print(JSON.toJSON(result));
	
	}

然后就是保存你所获取到的拍照图片:

/**
	 * 解码生成图片
	 * @param request
	 * @param response
	 */
	public boolean GenerateImage(String imgStr, String filePath, String fileName) {
	    try {
	        if (imgStr == null) {
	            return false;
	        }
	        BASE64Decoder decoder = new BASE64Decoder();
	        //Base64解码
	        byte[] b = decoder.decodeBuffer(imgStr);
	        //如果目录不存在,则创建
	        File file = new File(filePath);
	        if (!file.exists()) {
	            file.mkdirs();
	        }
	        //生成图片
	        OutputStream out = new FileOutputStream(filePath + fileName);
	        out.write(b);
	        out.flush();
	        out.close();
	        return true;
	    } catch (Exception e) {
	        return false;
	    }
	
	}

运行结果:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用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. 在程序结束时关闭摄像头。 注意:在使用该示例代码之前,请先确保电脑上已经安装了摄像头并且能够正常使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值