📄 jcamera.java
字号:
import java.awt.Component;
import java.awt.Image;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
/*
* 创建日期 2006-11-19
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class JCamera {
private static Player player = null;
private static Buffer buf = null;
private static Image img = null;
private static BufferToImage btoi = null;
private static Component comp;
public static Component init(){
CaptureDeviceInfo di = null;
MediaLocator ml = null;
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
if ((comp = player.getVisualComponent()) != null)
{
return comp;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static Image getImg(){
// 从视频得到图像
FrameGrabbingControl fgc =
(FrameGrabbingControl) player.getControl(
"javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
return img;
}
public static Component getComp(){
return comp;
}
public static void detectCaptureDevices() {
// check if JavaSound capture is available
// message("Looking for Audio capturer");
Class dsauto = null;
try {
dsauto = Class.forName("DirectSoundAuto");
dsauto.newInstance();
// message("Finished detecting DirectSound capturer");
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t ) {
}
Class jsauto = null;
try {
jsauto = Class.forName("JavaSoundAuto");
jsauto.newInstance();
// message("Finished detecting javasound capturer");
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t ) {
// message("JavaSound capturer detection failed!");
}
// Check if VFWAuto or SunVideoAuto is available
// message("Looking for video capture devices");
Class auto = null;
Class autoPlus = null;
try {
auto = Class.forName("VFWAuto");
} catch (Exception e) {
}
if (auto == null) {
try {
auto = Class.forName("SunVideoAuto");
} catch (Exception ee) {
}
try {
autoPlus = Class.forName("SunVideoPlusAuto");
} catch (Exception ee) {
}
}
try {
Object instance = auto.newInstance();
if (autoPlus != null) {
Object instancePlus = autoPlus.newInstance();
}
// message("Finished detecting video capture devices");
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
// message("Capture device detection failed!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -