Posted on 2012-06-09 01:04 
penngo 阅读(4924) 
评论(2)  编辑  收藏  所属分类: 
Java 
			 
			
		 
		本来sun有个jmf组件可以很方便的实现摄像头截图的,不过这版本后来停止更新了,当前官网最新版本为Java Media Framework (JMF) 2.1.1e,下载回来,在windows 7 32位上使用,居然不能运行,网上另外找了个jmf的替代框架fmj使用,截图实现代码: 
 package com.pengo.capture;
package com.pengo.capture;

 import java.awt.BorderLayout;
import java.awt.BorderLayout;
 import java.awt.Dimension;
import java.awt.Dimension;
 import java.awt.Graphics2D;
import java.awt.Graphics2D;
 import java.awt.event.ActionEvent;
import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
import java.awt.event.ActionListener;
 import java.awt.image.BufferedImage;
import java.awt.image.BufferedImage;
 import java.io.File;
import java.io.File;
 import java.io.IOException;
import java.io.IOException;
 import javax.imageio.ImageIO;
import javax.imageio.ImageIO;
 import javax.media.MediaLocator;
import javax.media.MediaLocator;
 import javax.swing.JButton;
import javax.swing.JButton;
 import javax.swing.JFrame;
import javax.swing.JFrame;
 import javax.swing.JPanel;
import javax.swing.JPanel;
 import javax.swing.JTextField;
import javax.swing.JTextField;
 import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
 import net.sf.fmj.ui.application.ContainerPlayer;
import net.sf.fmj.ui.application.ContainerPlayer;
 import net.sf.fmj.ui.application.PlayerPanelPrefs;
import net.sf.fmj.ui.application.PlayerPanelPrefs;


 public class CameraFrame extends JFrame
public class CameraFrame extends JFrame {
{
 private static int num = 0;
    private static int num = 0;

 public CameraFrame() throws Exception
    public CameraFrame() throws Exception {
{
 this.setTitle("摄像头截图应用");
        this.setTitle("摄像头截图应用");
 this.setSize(480, 500);
        this.setSize(480, 500);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 final JPanel cameraPanel = new JPanel();
        final JPanel cameraPanel = new JPanel();
 this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().setLayout(new BorderLayout());
 this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
        this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
 ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
        ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
 MediaLocator locator = CaptureDeviceBrowser.run(null);   //弹出摄像头设备选择
        MediaLocator locator = CaptureDeviceBrowser.run(null);   //弹出摄像头设备选择
 
        
 //        MediaLocator locator = null;
//        MediaLocator locator = null;
 //        GlobalCaptureDevicePlugger.addCaptureDevices();
//        GlobalCaptureDevicePlugger.addCaptureDevices();
 //        Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);
//        Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);
 //        if (vectorDevices == null || vectorDevices.size() == 0)
//        if (vectorDevices == null || vectorDevices.size() == 0)
 //        {
//        {
 //            System.out.println("没有摄像头===");
//            System.out.println("没有摄像头===");
 //            return;
//            return;
 //        }
//        }
 //        //选择第一个摄像头设备
//        //选择第一个摄像头设备
 //        for ( int i = 0;  i < vectorDevices.size();  i++ )
//        for ( int i = 0;  i < vectorDevices.size();  i++ ) 
 //        {
//        {
 //            CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
//            CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
 //            System.out.println("设备名===============" + infoCaptureDevice.getName());
//            System.out.println("设备名===============" + infoCaptureDevice.getName());
 //            //选择第一个设备为程序使用,如果存在多个设备时,则第一个可能不是摄像头
//            //选择第一个设备为程序使用,如果存在多个设备时,则第一个可能不是摄像头
 //            locator = infoCaptureDevice.getLocator();
//            locator = infoCaptureDevice.getLocator();
 //            break;
//            break;
 //        }
//        }

 PlayerPanelPrefs prefs = new PlayerPanelPrefs();
        PlayerPanelPrefs prefs = new PlayerPanelPrefs();
 containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
        containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
 
        
 JPanel btnPanel = new JPanel(new BorderLayout());
        JPanel btnPanel = new JPanel(new BorderLayout());
 final JTextField path = new JTextField("E:\\camera");
        final JTextField path = new JTextField("E:\\camera");
 path.setColumns(30);
        path.setColumns(30);
 btnPanel.add(path, BorderLayout.WEST);
        btnPanel.add(path, BorderLayout.WEST);
 JButton okBtn = new JButton("截图");
        JButton okBtn = new JButton("截图");

 okBtn.addActionListener(new ActionListener()
        okBtn.addActionListener(new ActionListener() {
{

 public void actionPerformed(ActionEvent e)
             public void actionPerformed(ActionEvent e) {
{
 Dimension imageSize = cameraPanel.getSize();
                 Dimension imageSize = cameraPanel.getSize();
 BufferedImage image = new BufferedImage(imageSize.width,
                    BufferedImage image = new BufferedImage(imageSize.width,
 imageSize.height, BufferedImage.TYPE_INT_ARGB);
                            imageSize.height, BufferedImage.TYPE_INT_ARGB);
 Graphics2D g = image.createGraphics();
                    Graphics2D g = image.createGraphics();
 cameraPanel.paint(g);
                    cameraPanel.paint(g);
 g.dispose();
                    g.dispose();

 try
                    try  {
{
 
               
 String filePath = path.getText();
                        String filePath = path.getText();
 File file = new File(filePath);
                        File file = new File(filePath);

 if(file.exists() == false)
                        if(file.exists() == false) {
{
 file.mkdirs();
                            file.mkdirs();
 }
                        }
 ImageIO.write(image, "png", new File(file.getAbsolutePath() + "/" + num + ".png"));
                        ImageIO.write(image, "png", new File(file.getAbsolutePath() + "/" + num + ".png"));
 num++;
                        num++;

 } catch (IOException ex)
                    } catch (IOException ex)  {
{
 ex.printStackTrace();
                        ex.printStackTrace();
 
                       
 }
                    }
 }
             }
 });
        });
 btnPanel.add(okBtn, BorderLayout.EAST);
        btnPanel.add(okBtn, BorderLayout.EAST);
 this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
        this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
 }
    }
 
    

 public static void main(String[] args) throws Exception
    public static void main(String[] args) throws Exception {
{
 CameraFrame camera = new CameraFrame();
        CameraFrame camera = new CameraFrame();
 camera.setVisible(true);
        camera.setVisible(true);
 }
    }
 }
}