服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现摄像头截图功能

java实现摄像头截图功能

2021-06-04 13:53penngo Java教程

这篇文章主要为大家详细介绍了java实现摄像头截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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

延伸 · 阅读

精彩推荐