欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现摄像头截图功能

程序员文章站 2024-03-31 13:54:58
本文为大家分享了java摄像头截图的具体代码,供大家参考,具体内容如下 本来sun有个jmf组件可以很方便的实现摄像头截图的,不过这版本后来停止更新了,当前官网最新版本为...

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

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