jmf实现调用笔记本摄像头并拍照

最近在学习jmf的相关功能,尝试了用它实现视频通信,因此,先做了调用摄像头的工作,在网上找个很久的资料,终于发现了一个能用的代码,特与大家分享

   前期工作:jmf安装

                    jmf相关包的下载(最好加载原始的五个包)

                    eclipse下运行

项目下载地址:http://pan.baidu.com/s/1ntoGZbz

代码如下:


package shexiangtou;


import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.image.BufferedImage; 
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; import javax.imageio.ImageIO;
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.protocol.vfw.DataSource;


public class FirstCapture extends JPanel implements ActionListener{ private CaptureDeviceInfo captureDeviceInfo=null; 
private MediaLocator mediaLocator=null; 
private static Player player=null; 
//private ImagePanel imagePanel=null; 
private JButton capture; 
private Buffer buffer=null; 
private VideoFormat videoFormat=null; 
private BufferToImage bufferToImage=null; 
private Image image=null; 


public FirstCapture() 





setLayout(new BorderLayout()); 
setSize(320,550); captureDeviceInfo=CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0"); //这里放置的是视频驱动


mediaLocator=new MediaLocator("vfw://0"); //这里是视频地址
//imagePanel=new ImagePanel(); 
capture=new JButton("拍照"); 
capture.addActionListener(this); 
DataSource ds = new DataSource();
ds.setLocator(mediaLocator);
try { 
player=Manager.createRealizedPlayer(mediaLocator); 
player.start(); 
Component comp; 
Component comp1; 
if((comp=player.getVisualComponent())!=null) 
add(comp,BorderLayout.NORTH); 
// if((comp1=player.getControlPanelComponent())!=null) 
// add(comp1,BorderLayout.CENTER); 
} 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(); 
System.out.println("ioe");

//add(imagePanel,BorderLayout.SOUTH); 
add(capture,BorderLayout.CENTER); 



@Override
public void print(Graphics g) { 
// TODO Auto-generated method stub 
super.print(g); 
g.setColor(new Color(255,0,0)); 
g.drawLine(0, 0, 100, 100); 
} public static void main(String[] args) { 
// TODO Auto-generated method stub 
JFrame f = new JFrame("FirstCapture"); 
FirstCapture cf = new FirstCapture(); 


f.addWindowListener(new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
player.close(); 
System.exit(0);}}); 


f.add("Center",cf); 
f.pack(); 
f.setSize(new Dimension(320,550)); 
f.setVisible(true); 



public void actionPerformed(ActionEvent e) { 
// TODO Auto-generated method stub 
FrameGrabbingControl fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl"); 
buffer=fgc.grabFrame(); 
bufferToImage=new BufferToImage((VideoFormat)buffer.getFormat()); 
image=bufferToImage.createImage(buffer); 
try {
ImageIO.write((RenderedImage) image, "jpg", new File("F:\\test.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// public static void saveImage(Image image,String path) 
// { 
// BufferedImage bi=new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB); 
// Graphics2D g2 = bi.createGraphics(); 
// g2.drawImage(image, null, null); 
// FileOutputStream fos=null; 
// try { 
// fos=new FileOutputStream(path); 
// 
// } catch (FileNotFoundException e) { 
// // TODO Auto-generated catch block 
// e.printStackTrace(); 
// } 
// 
// } 
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值