java实现摄像头截图功能

作者:penngo 时间:2023-12-01 19:53:22 

本文为大家分享了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摄像头截图

来源:http://www.blogjava.net/pengo/archive/2012/06/09/380385.html

标签:java,摄像头,截图
0
投稿

猜你喜欢

  • Spring Data环境搭建实现过程解析

    2022-02-26 20:13:38
  • android 点击EditText始终不弹出软件键盘实现代码

    2022-05-23 06:37:19
  • 深入探讨linux下进程的最大线程数、进程最大数、进程打开的文件数

    2021-07-06 00:49:15
  • java8到java15的新功能简介

    2023-07-28 02:18:18
  • Java 字符串转float运算 float转字符串的方法

    2022-04-09 10:09:06
  • Eclipse下基于Java的OpenCV开发环境配置教程

    2022-01-30 21:02:54
  • Mybatis初始化知识小结

    2023-11-01 13:59:27
  • Java中的内存泄露问题和解决办法

    2022-05-12 20:02:35
  • C++类静态成员与类静态成员函数详解

    2022-10-10 08:22:20
  • C#实现winform渐变效果的方法

    2023-03-14 00:26:06
  • Eclipse中maven的配置详解

    2023-01-04 08:51:09
  • Android实现读写USB串口数据

    2023-10-21 19:12:56
  • java中synchronized锁的升级过程

    2023-12-01 13:14:51
  • OnSharedPreferenceChangeListener详解及出现不触发解决办法

    2021-10-25 19:06:36
  • C++编程异常处理中try和throw以及catch语句的用法

    2023-04-08 15:29:41
  • C#实现排列组合算法完整实例

    2023-04-16 09:36:59
  • C#获取本地IP的四种方式示例详解

    2023-04-16 00:52:50
  • SPRING IOC注入方式过程解析

    2023-10-11 12:59:13
  • java 中遍历取值异常(Hashtable Enumerator)解决办法

    2023-08-06 05:17:08
  • spring boot使用thymeleaf为模板的基本步骤介绍

    2023-12-13 15:07:23
  • asp之家 软件编程 m.aspxhome.com