?? captureframe.java
字號:
package bin;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.lang.*;
class CaptureFrame extends JFrame
{
public static Player player = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;
private Image image;
private VideoFormat vf = null;
private FrameGrabbingControl fgc = null;
private Buffer buf = null;
private Image img = null;
private BufferToImage btoi = null;
//初始化
CaptureFrame()
{
try{
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
// System.out.println( "init1" ) ;
ml = di.getLocator();
// System.out.println( "init2" ) ;
}
catch (Exception e)
{
e.printStackTrace();
}
// playerOpen();
try{
Thread.sleep(5000);
}
catch(Exception e){}
}
public void playerOpen()
{
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
}
catch (Exception e)
{
System.out.println("無法檢測攝像頭3");
e.printStackTrace();
}
}
public void playerclose()
{
player.close();
try{
Thread.sleep(1000);
}catch(Exception ew){}
player.deallocate();
}
public BufferedImage capture(int width,int height,String name)
{
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame(); // Convert it to an image
vf = (VideoFormat) buf.getFormat();
btoi = new BufferToImage(vf);
img = btoi.createImage(buf); // show the image
BufferedImage bufimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
bufimg.getGraphics().drawImage(img,0,0,width,height,null);
try{
FileOutputStream out=new FileOutputStream(name+".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufimg);
out.close();
}catch(Exception e){System.out.print("圖像保存錯誤");}
return bufimg;
}
public BufferedImage capture(int width,int height)
{
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame(); // Convert it to an image
vf = (VideoFormat) buf.getFormat();
btoi = new BufferToImage(vf);
img = btoi.createImage(buf); // show the image
BufferedImage bufimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
bufimg.getGraphics().drawImage(img,0,0,width,height,null);
return bufimg;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -