日期:2014-05-17  浏览次数:20677 次

JMF真的就这样难吗?
最近项目要求能对视频和语音进行同步合成,以及对视频进行画面与语音的分离功能,由于没有接触过这类东西,所以想从最基础做起,于是在网上找到sun公司开源的jmf架构,看到网上也有不少小例子,就开始对安装jmf并配置了开发环境,将小例子粘到自己的新建工程下,结果发现不好用,没办法自己也顺手写了一个都发生同样的问题,希望有经验的朋友能够帮助解决一下。 
  我现在干脆就用网上的一个例子来说明问题,实现的功能是,利用点击拍照按钮能,启动摄像头并将捕捉摄像头的画面将生成图片保存到物理硬盘中,代码如下: 
Java code
package com.creatplay;
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.Panel;  
import java.awt.Rectangle;  
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.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
 
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.image.codec.jpeg.ImageFormatException;  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGEncodeParam;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
 
 
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);  
        String str="vfw:Microsoft WDM Image Capture (Win32):0";  
        captureDeviceInfo=CaptureDeviceManager.getDevice(str);  
       // MediaLocator mediaLocator=captureDeviceInfo.getLocator();
        mediaLocator=new MediaLocator("vfw://0");  
        imagePanel=new ImagePanel();  
         capture=new JButton("拍照");  
         capture.addActionListener(this);  
        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();  
        }  
        add(imagePanel,BorderLayout.SOUTH);  
        add(capture,BorderLayout.CENTER);  
    }  
      
    @Override 
    public void print(Graphics g) {  
        // TODO Auto-generated method stub  
        super.print(g);  
        g.setColor(n