java实现摄像头截图功能

 更新时间:2021年04月22日 10:04:10   作者:penngo  
这篇文章主要为大家详细介绍了java实现摄像头截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了java摄像头截图的具体代码,供大家参考,具体内容如下

本来sun有个jmf组件可以很方便的实现摄像头截图的,不过这版本后来停止更新了,当前官网最新版本为Java Media Framework (JMF) 2.1.1e,下载回来,在windows 7 32位上使用,居然不能运行,网上另外找了个jmf的替代框架fmj使用,截图实现代码:

package com.pengo.capture;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.MediaLocator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.ui.application.ContainerPlayer;
import net.sf.fmj.ui.application.PlayerPanelPrefs;

public class CameraFrame extends JFrame{
  private static int num = 0;
  public CameraFrame() throws Exception{
    this.setTitle("摄像头截图应用");
    this.setSize(480, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JPanel cameraPanel = new JPanel();
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
    ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
    MediaLocator locator = CaptureDeviceBrowser.run(null);  //弹出摄像头设备选择
    
//    MediaLocator locator = null;
//    GlobalCaptureDevicePlugger.addCaptureDevices();
//    Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);
//    if (vectorDevices == null || vectorDevices.size() == 0)
//    {
//      System.out.println("没有摄像头===");
//      return;
//    }
//    //选择第一个摄像头设备
//    for ( int i = 0; i < vectorDevices.size(); i++ ) 
//    {
//      CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
//      System.out.println("设备名===============" + infoCaptureDevice.getName());
//      //选择第一个设备为程序使用,如果存在多个设备时,则第一个可能不是摄像头
//      locator = infoCaptureDevice.getLocator();
//      break;
//    }

    PlayerPanelPrefs prefs = new PlayerPanelPrefs();
    containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
    
    JPanel btnPanel = new JPanel(new BorderLayout());
    final JTextField path = new JTextField("E:\\camera");
    path.setColumns(30);
    btnPanel.add(path, BorderLayout.WEST);
    JButton okBtn = new JButton("截图");
    okBtn.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
         Dimension imageSize = cameraPanel.getSize();
          BufferedImage image = new BufferedImage(imageSize.width,
              imageSize.height, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g = image.createGraphics();
          cameraPanel.paint(g);
          g.dispose();
          try {
        
            String filePath = path.getText();
            File file = new File(filePath);
            if(file.exists() == false){
              file.mkdirs();
            }
            ImageIO.write(image, "png", new File(file.getAbsolutePath() + "/" + num + ".png"));
            num++;
          } catch (IOException ex) {
            ex.printStackTrace();
            
          }
       }
    });
    btnPanel.add(okBtn, BorderLayout.EAST);
    this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
  }
  
  public static void main(String[] args) throws Exception{
    CameraFrame camera = new CameraFrame();
    camera.setVisible(true);
  }
}

源码下载:java摄像头截图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java SSH 秘钥连接mysql数据库的方法

    Java SSH 秘钥连接mysql数据库的方法

    这篇文章主要介绍了Java SSH 秘钥连接mysql数据库的方法,包括引入依赖的代码和出现异常报错问题,需要的朋友可以参考下
    2021-06-06
  • Springdoc替换swagger的实现步骤分解

    Springdoc替换swagger的实现步骤分解

    最近在spring看到的,spring要对api文档动手了,有些人说swagger不好用,其实也没那么不好用,有人说代码还是有点侵入性,这倒是真的,我刚试了springdoc可以说还是有侵入性但是也可以没有侵入性,这就看你对文档有什么要求了
    2023-02-02
  • Java 发送http请求上传文件功能实例

    Java 发送http请求上传文件功能实例

    本文通过实例代码给大家介绍了Java 发送http请求上传文件功能,需要的朋友参考下吧
    2017-06-06
  • 解决springboot+activemq启动报注解错误的问题

    解决springboot+activemq启动报注解错误的问题

    这篇文章主要介绍了解决springboot+activemq启动报注解错误的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 浅谈xml配置spring profiles的几个注意点

    浅谈xml配置spring profiles的几个注意点

    这篇文章主要介绍了浅谈xml配置spring profiles的几个注意点,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • SpringBoot 使用Mybatis分页插件实现详解

    SpringBoot 使用Mybatis分页插件实现详解

    这篇文章主要介绍了SpringBoot 使用Mybatis分页插件实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Flink ExecutionGraph生成源码解析

    Flink ExecutionGraph生成源码解析

    这篇文章主要为大家介绍了Flink ExecutionGraph生成源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 利用ClasserLoader实现jar包加载并调用里面的方法

    利用ClasserLoader实现jar包加载并调用里面的方法

    classloader即是类加载,虚拟机把描述类的数据从class字节码文件加载到内存,并对数据进行检验、转换解析和初始化,了解java的类加载机制,可以快速解决运行时的各种加载问题并快速定位其背后的本质原因,本文介绍了如何利用ClasserLoader来实现jar包加载并调用里面的方法
    2024-09-09
  • SpringBoot整合EasyExcel实现文件导入导出

    SpringBoot整合EasyExcel实现文件导入导出

    这篇文章主要介绍了SpringBoot整合EasyExcel实现文件导入导出的方法,帮助大家更好的理解和学习使用SpringBoot,感兴趣的朋友可以了解下
    2021-05-05
  • Flink开发IDEA环境搭建与测试的方法

    Flink开发IDEA环境搭建与测试的方法

    这篇文章主要介绍了Flink开发IDEA环境搭建与测试的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03

最新评论