Chinaunix首页 | 论坛 | 博客
  • 博客访问: 691128
  • 博文数量: 118
  • 博客积分: 2933
  • 博客等级: 少校
  • 技术积分: 1779
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-27 10:16
文章存档

2013年(35)

2012年(27)

2011年(23)

2010年(33)

分类: Java

2013-04-25 15:30:31

1.用Java Media Framework
下载地址:
2.设置环境变量
新建JMFHOME=C:\Program Files\JMF2.1.1e
设置CLASSPATH=%JMFHOME%\lib\jmf.jar;%JMFHOME%\lib\sound.jar;.;%CLASSPATH% 
3.导入JMF所需jar包customizer.jar
,jmf.jar,mediaplayer.jar,multiplayer.jar,sound.jar

点击(此处)折叠或打开

  1. package com.util;

  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Graphics;
  5. import java.awt.GraphicsConfiguration;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.HeadlessException;
  9. import java.awt.Image;
  10. import java.awt.Transparency;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.awt.image.BufferedImage;
  16. import java.awt.image.ColorModel;
  17. import java.awt.image.PixelGrabber;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.Date;
  21. import java.util.Vector;

  22. import javax.imageio.ImageIO;
  23. import javax.media.Buffer;
  24. import javax.media.CaptureDeviceInfo;
  25. import javax.media.CaptureDeviceManager;
  26. import javax.media.Format;
  27. import javax.media.MediaLocator;
  28. import javax.media.NoPlayerException;
  29. import javax.media.control.FrameGrabbingControl;
  30. import javax.media.format.RGBFormat;
  31. import javax.media.format.VideoFormat;
  32. import javax.media.protocol.DataSource;
  33. import javax.media.protocol.PushBufferDataSource;
  34. import javax.media.util.BufferToImage;
  35. import javax.swing.ImageIcon;
  36. import javax.swing.JButton;
  37. import javax.swing.JFrame;
  38. import javax.swing.JPanel;

  39. import profile.PearlInputHtml;

  40. import jmapps.jmstudio.CaptureDialog;
  41. import jmapps.ui.PlayerFrame;
  42. import jmapps.util.CDSWrapper;
  43. import jmapps.util.JMFUtils;

  44. public class Capture extends PlayerFrame {
  45.     public static String picUrl="";
  46.     public boolean flag=false;
  47.     
  48.     
  49.     public Capture() {
  50.         super(null, "视频捕获窗口");
  51.     }
  52.     DataSource dataSource;
  53.     private CaptureDeviceInfo infor;
  54.     private MediaLocator mediaLocator;
  55.     String str1 = "vfw:Logitech USB Video Camera:0";
  56.     String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
  57.     private String url = "vfw:Microsoft WDM Image Capture (Win32):0";
  58.     private Component com;
  59.     private JPanel panel;
  60.     private int captureCount = 0;
  61.     FrameGrabbingControl controlGrabber;
  62.     public void play() {
  63.         if (mediaPlayerCurrent.getState() != mediaPlayerCurrent.Started) {
  64.             mediaPlayerCurrent.start();
  65.         }
  66.     }

  67.     public void init() throws NoPlayerException, IOException {
  68.         // setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  69.         String nameCaptureDeviceAudio = null;
  70.         String nameCaptureDeviceVideo = null;
  71.         CaptureDialog dialogCapture = new CaptureDialog(this, null);
  72.         dialogCapture.show();
  73.         if (dialogCapture.getAction() == CaptureDialog.ACTION_CANCEL)
  74.             return;
  75.         CaptureDeviceInfo cdi = dialogCapture.getAudioDevice();
  76.         if (cdi != null && dialogCapture.isAudioDeviceUsed())
  77.             nameCaptureDeviceAudio = cdi.getName();
  78.         cdi = dialogCapture.getVideoDevice();
  79.         if (cdi != null && dialogCapture.isVideoDeviceUsed())
  80.             nameCaptureDeviceVideo = cdi.getName();
  81.         dataSource = JMFUtils.createCaptureDataSource(nameCaptureDeviceAudio,
  82.                 dialogCapture.getAudioFormat(), nameCaptureDeviceVideo,
  83.                 dialogCapture.getVideoFormat());
  84.         DataSource cdswrapper = new CDSWrapper(
  85.                 (PushBufferDataSource) dataSource);
  86.         dataSource = cdswrapper;
  87.         dataSource.connect();
  88.         open(dataSource);
  89.         JPanel controlPanel = new JPanel();
  90.         controlPanel.setName("controlPnael is here");
  91.         add(BorderLayout.SOUTH, controlPanel);
  92.         JButton capture = new JButton("Capture Image");
  93.         capture.addActionListener(new ActionListener() {
  94.             public void actionPerformed(ActionEvent arg0) {
  95.                 mediaPlayerCurrent.stop();
  96.                 Buffer bufferFrame;
  97.                 BufferToImage bufferToImage;
  98.                 Image image;
  99.                 BufferedImage bi;
  100.                 controlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
  101.                         .getControl("javax.media.control.FrameGrabbingControl");
  102.                 bufferFrame = controlGrabber.grabFrame();
  103.                 bufferToImage = new BufferToImage((VideoFormat) bufferFrame.getFormat());
  104.                 image = bufferToImage.createImage(bufferFrame);
  105.                 String desFileName = "img_"+ ComUtil.getFormatDate(new Date(),"yyyyMMddHHMMSSSS");// 拼成文件名称
  106.                 String url = ConstantUtil.imgUploadAddress + desFileName;
  107.                 //File out = new File("capture" + (++captureCount) + ".png");
  108.                 
  109.                 picUrl = url+".png";
  110.                 File out = new File(url+".png");
  111.                 try {
  112.                     bi = toBufferedImage(image);
  113.                     ImageIO.write(bi, "png", out);
  114.                     
  115.                     File fileTmp = new File(picUrl);
  116.                     Image m = new ImageIcon(picUrl).getImage();
  117.                     BufferedImage bufImg = new BufferedImage(
  118.                             PearlInputHtml.capLabel.getWidth() - 2,
  119.                             PearlInputHtml.capLabel.getHeight() - 2,
  120.                             BufferedImage.TYPE_INT_RGB);
  121.                     bufImg.getGraphics().drawImage(m, 0, 0,
  122.                             PearlInputHtml.capLabel.getWidth() - 2,
  123.                             PearlInputHtml.capLabel.getHeight() - 2, null);
  124.                     PearlInputHtml.capimgUrlField.setText(desFileName);
  125.                     try {
  126.                         m = ImageIO.read(fileTmp);
  127.                         if (m != null) {
  128.                             PearlInputHtml.capLabel.setIcon(new ImageIcon(bufImg));
  129.                         } else {
  130.                         }
  131.                     } catch (IOException e1) {
  132.                         e1.printStackTrace();
  133.                     }
  134.                     
  135.                     
  136.                 } catch (IOException e1) {
  137.                     e1.printStackTrace();
  138.                 }
  139.                 mediaPlayerCurrent.start();
  140.             }
  141.         });
  142.         controlPanel.add(BorderLayout.CENTER, capture);
  143.         JButton playStop = new JButton("stop");
  144.         // add(BorderLayout.SOUTH,playControl);
  145.         playStop.addActionListener(new ActionListener() {
  146.             // @Override
  147.             public void actionPerformed(ActionEvent arg0) {
  148.                 mediaPlayerCurrent.stop();
  149.             }
  150.         });
  151.         controlPanel.add(BorderLayout.EAST, playStop);
  152.         JButton playStart = new JButton("start");
  153.         // add(BorderLayout.SOUTH,playControl);
  154.         playStart.addActionListener(new ActionListener() {

  155.             // @Override
  156.             public void actionPerformed(ActionEvent arg0) {
  157.                 // mediaPlayerCurrent.stop();
  158.                 if (mediaPlayerCurrent.getState() != mediaPlayerCurrent.Started) {
  159.                     mediaPlayerCurrent.start();
  160.                 }
  161.             }
  162.         });
  163.         controlPanel.add(BorderLayout.WEST, playStart);
  164.         addWindowListener(new WindowAdapter() {
  165.             // @Override
  166.             public void windowClosing(WindowEvent e) {
  167.                 mediaPlayerCurrent.close();
  168.                 dataSource.disconnect();
  169.                 System.out.println("exit.....");
  170.                 this.windowClosed(e);
  171.                 //System.exit(0);
  172.             }
  173.         });
  174.     }

  175.     public static boolean hasAlpha(Image image) {
  176.         // If buffered image, the color model is readily available
  177.         if (image instanceof BufferedImage) {
  178.             BufferedImage bimage = (BufferedImage) image;
  179.             return bimage.getColorModel().hasAlpha();
  180.         }
  181.         // Use a pixel grabber to retrieve the image's color model;
  182.         // grabbing a single pixel is usually sufficient
  183.         PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
  184.         try {
  185.             pg.grabPixels();
  186.         } catch (InterruptedException e) {
  187.         }

  188.         // Get the image's color model
  189.         ColorModel cm = pg.getColorModel();
  190.         return cm.hasAlpha();
  191.     }

  192.     public static BufferedImage toBufferedImage(Image image) {
  193.         if (image instanceof BufferedImage) {
  194.             return (BufferedImage) image;
  195.         }

  196.         // This code ensures that all the pixels in the image are loaded
  197.         image = new ImageIcon(image).getImage();

  198.         // Determine if the image has transparent pixels; for this method's
  199.         // implementation, see e661 Determining If an Image Has Transparent
  200.         // Pixels
  201.         boolean hasAlpha = hasAlpha(image);

  202.         // Create a buffered image with a format that's compatible with the
  203.         // screen
  204.         BufferedImage bimage = null;
  205.         GraphicsEnvironment ge = GraphicsEnvironment
  206.                 .getLocalGraphicsEnvironment();
  207.         try {
  208.             // Determine the type of transparency of the new buffered image
  209.             int transparency = Transparency.OPAQUE;
  210.             if (hasAlpha) {
  211.                 transparency = Transparency.BITMASK;
  212.             }

  213.             // Create the buffered image
  214.             GraphicsDevice gs = ge.getDefaultScreenDevice();
  215.             GraphicsConfiguration gc = gs.getDefaultConfiguration();
  216.             bimage = gc.createCompatibleImage(image.getWidth(null), image
  217.                     .getHeight(null), transparency);
  218.         } catch (HeadlessException e) {
  219.             // The system does not have a screen
  220.             System.err.println("The system does not have a screen!");
  221.             System.exit(-1);
  222.         }

  223.         if (bimage == null) {
  224.             // Create a buffered image using the default color model
  225.             int type = BufferedImage.TYPE_INT_RGB;
  226.             if (hasAlpha) {
  227.                 type = BufferedImage.TYPE_INT_ARGB;
  228.             }
  229.             bimage = new BufferedImage(image.getWidth(null), image
  230.                     .getHeight(null), type);
  231.         }

  232.         // Copy image to buffered image
  233.         Graphics g = bimage.createGraphics();

  234.         // Paint the image onto the buffered image
  235.         g.drawImage(image, 0, 0, null);
  236.         g.dispose();

  237.         return bimage;
  238.     }

  239.     public MediaLocator autoDetect() {// 自动识别功能函数
  240.         MediaLocator ml = null; // 视频采集设备对应的MediaLocator
  241.         VideoFormat currentFormat = null;// 用户定制获得视频采集设备支持的格式
  242.         Format setFormat = null;// 用户定制视频采集设备输出的格式
  243.         Format[] videoFormats = null;// 视频采集设备支持的所有格式
  244.         System.out.println(" AutoDetect for VFW");// VFW:微软的 Video for Windows
  245.         // 获得当前所有设备列表
  246.         Vector deviceList = CaptureDeviceManager.getDeviceList(null);
  247.         CaptureDeviceInfo device = CaptureDeviceManager.getDevice(url);
  248.         if (deviceList != null) {
  249.             // 根据设备列表,找出可用设备名称
  250.             for (int i = 0; i < deviceList.size(); i++) {
  251.                 try {
  252.                     CaptureDeviceInfo di = (CaptureDeviceInfo) deviceList
  253.                             .elementAt(i);
  254.                     // 如果设备名称以vfw开头
  255.                     if (di.getName().startsWith("vfw:")) {
  256.                         // 获得所有支持RGB格式
  257.                         videoFormats = di.getFormats();
  258.                         for (int j = 0; j < videoFormats.length; j++) {
  259.                             // 我们只需要第一种RGB格式
  260.                             if (videoFormats[j] instanceof RGBFormat) {
  261.                                 currentFormat = (RGBFormat) videoFormats[i];
  262.                                 break;
  263.                             }
  264.                         }
  265.                         if (currentFormat == null) {
  266.                             System.err.println("Search For RGBFormat Failed");
  267.                             System.exit(-1);
  268.                         }
  269.                         // 通过设备,获得MediaLocator,这个很重要
  270.                         ml = di.getLocator();
  271.                     }
  272.                 } catch (Exception npe) {
  273.                     System.err.println("Unable to get Processor for device");
  274.                     System.exit(-1);
  275.                 }
  276.             }
  277.         } else {
  278.             System.err.println("No Capture Device OK");
  279.             System.exit(-1);
  280.         }
  281.         mediaLocator = ml;
  282.         return ml;// 返回可用的设备medialocator
  283.     }

  284.     public static void main(String[] args) throws NoPlayerException,
  285.             IOException {
  286.         Capture demo = new Capture();
  287.         demo.setSize(100, 100);
  288.         demo.autoDetect();
  289.         demo.init();
  290.         demo.play();
  291.         demo.setVisible(true);
  292.     }
  293. }


阅读(11901) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~