zoukankan      html  css  js  c++  java
  • jmf天昏地暗之路(三)---抓取当前帧照片并保存为bmp格式(结束)

    断断续续弄了差不多两个星期,终于把这个弄好了,在我的电脑上以及朋友的xp系统上测试成功!

    下面不多说,直接上代码。

    说明:电脑必须装好jmf,并且用JMStudio能正常打开摄像头,否则运行不了,具体是什么原因我也不太清楚。

    //已成功实现功能,获取当前摄像头并且拍照。
    //照片存在E:photo目录下
    
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Date;
    
    import javax.media.Buffer;
    import javax.media.CannotRealizeException;
    import javax.media.CaptureDeviceInfo;
    import javax.media.CaptureDeviceManager;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.NoPlayerException;
    import javax.media.Player;
    import javax.media.control.FrameGrabbingControl;
    import javax.media.format.VideoFormat;
    import javax.media.util.BufferToImage;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    
    import com.sun.media.jai.codec.BMPEncodeParam;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageEncoder;
    
    
    
    
    public class CapturePicture extends JFrame implements ActionListener{
    	
    	private static CapturePicture jframe=new CapturePicture();
    	private JPanel showPhone=new JPanel();	//用来接受摄像头数据
    	private JPanel bpanel=new JPanel();
    	private JButton button=new JButton("拍照");
    	//摄像头驱动
    	private CaptureDeviceInfo captureDeviceInfo=null;
    	private MediaLocator mediaLocator=null;
    	private static Player player=null;
    	private Buffer buffer=null;
    	private VideoFormat videoFormat=null;
    	private BufferToImage bufferToImage=null;
    	private Image image=null;
    	
    	
    	
    	
    	//构造器
    	private CapturePicture()
    	{
    		
    	}
    	protected void Init()
    	{
    		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jframe.setBounds(200,100,600,600);
    		jframe.setVisible(true);
    		jframe.setTitle("拍照小程序");
    		jframe.setResizable(true);
    		jframe.setLayout(null);//绝对布局
    		jframe.add(showPhone);
    		jframe.add(bpanel);
    		bpanel.setBounds(0,500,100,100);
    		bpanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
    		bpanel.add(button);	
    		button.addActionListener(this);
    		
    		
    		String str="vfw:Microsoft WDM Image Capture (Win32):0";
    		captureDeviceInfo=CaptureDeviceManager.getDevice(str);
    		mediaLocator=new MediaLocator("vfw://0");
    		
    		try {
    			player=Manager.createRealizedPlayer(mediaLocator);
    			player.start();
    			Component comp;
    			if((comp=player.getVisualComponent())!=null)
    			comp.setBounds(0, 0, 700, 500);
    			this.add(comp);
    			   
    			
    			 
    		} catch (NoPlayerException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (CannotRealizeException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		//创建照片所用的文件夹
    		File photo=new File("E:\","photo");
    		photo.mkdir();
    		
    		
    		
    	}
    	
    	public static CapturePicture getCP()
    	{
    		return jframe;
    	}
    	//获取照片
    	private void takePhoto()
    	{	 
    		//获取照片
    		FrameGrabbingControl fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
    		buffer=fgc.grabFrame();
    		bufferToImage=new BufferToImage((VideoFormat)buffer.getFormat());
    		image=bufferToImage.createImage(buffer);
    		OutputStream out=null;
    		Date date=new Date();
    		Long dtime=date.getTime();
    		
    		
    		
    		
    		
    		try {
    			out=new FileOutputStream("E:\photo\"+dtime+".BMP");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			
    			e.printStackTrace();
    		}
    		//可能是改参数的地方
    		BMPEncodeParam param = new BMPEncodeParam();
    		ImageEncoder enc = ImageCodec.createImageEncoder("BMP", out, param);
    		try {
    			enc.encode((RenderedImage) image);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			out.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    			
    			
    	}
    		
    		
    	
    	
    	
    	
    	public static void main(String args[])
    	{
    		CapturePicture cp=CapturePicture.getCP();
    		cp.Init();
    	}
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		takePhoto();
    		
    	}
    
    	
    }
    


  • 相关阅读:
    vue 将毫秒转为日期
    element-ui 点击获取table的行索引
    LInux设置tomcat端口为80
    java引用传递和值传递
    java包装类的自动装箱及缓存
    理解JVM之java内存模型
    理解JVM之类加载机制
    理解JVM之内存分配以及分代思想实现
    理解JVM之垃圾回收
    理解JVM之对象的生命周期
  • 原文地址:https://www.cnblogs.com/qq84435/p/3664846.html
Copyright © 2011-2022 走看看