本文為大家分享了java攝像頭截圖的具體代碼,供大家參考,具體內(nèi)容如下
本來(lái)sun有個(gè)jmf組件可以很方便的實(shí)現(xiàn)攝像頭截圖的,不過(guò)這版本后來(lái)停止更新了,當(dāng)前官網(wǎng)最新版本為java media framework (jmf) 2.1.1e,下載回來(lái),在windows 7 32位上使用,居然不能運(yùn)行,網(wǎng)上另外找了個(gè)jmf的替代框架fmj使用,截圖實(shí)現(xiàn)代碼:
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( "攝像頭截圖應(yīng)用" ); 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 ); //彈出攝像頭設(shè)備選擇 // medialocator locator = null; // globalcapturedeviceplugger.addcapturedevices(); // vector vectordevices = capturedevicemanager.getdevicelist(null); // if (vectordevices == null || vectordevices.size() == 0) // { // system.out.println("沒(méi)有攝像頭==="); // return; // } // //選擇第一個(gè)攝像頭設(shè)備 // for ( int i = 0; i < vectordevices.size(); i++ ) // { // capturedeviceinfo infocapturedevice = (capturedeviceinfo) vectordevices.get(i); // system.out.println("設(shè)備名===============" + infocapturedevice.getname()); // //選擇第一個(gè)設(shè)備為程序使用,如果存在多個(gè)設(shè)備時(shí),則第一個(gè)可能不是攝像頭 // 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攝像頭截圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.blogjava.net/pengo/archive/2012/06/09/380385.html