⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 captureframe.java

📁 SNP算法: 基于背景缩减和阴影检测的统计方法,适用于室内检测.
💻 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 + -