评论

收藏

[Java] java实现摄像头截图功能

编程语言 编程语言 发布于:2021-10-08 11:50 | 阅读数:488 | 评论:0

这篇文章主要为大家详细介绍了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摄像头截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:http://www.blogjava.net/pengo/archive/2012/06/09/380385.html

关注下面的标签,发现更多相似文章