首先安装jmf,安装过程中会提示注册摄像头.

首先安装jmf,安装过程中会提示注册摄像头.

开始代码

用applet比较快

001 package com.cayden.video;
002  
003 import java.awt.BorderLayout;
004 import java.awt.Choice;
005 import java.awt.Component;
006 import java.util.Vector;
007  
008 import javax.media.CaptureDeviceInfo;
009 import javax.media.CaptureDeviceManager;
010 import javax.media.Format;
011 import javax.media.Manager;
012 import javax.media.MediaLocator;
013 import javax.media.Player;
014 import javax.media.format.VideoFormat;
015 import javax.swing.JApplet;
016 import javax.swing.JPanel;
017  
018 public classVApplet extends JApplet {
019  
020     privateJPanel jContentPane = null;
021       
022     privateChoice choice = null;
023       
024     publicVApplet() {
025         super();
026     }
027       
028     publicvoid init() {
029         this.setSize(640,480);
030         this.setContentPane(getJContentPane());
031         this.setName("VApplet");
032     }
033     // 取系统所有可采集的硬件设备列表
034     privateCaptureDeviceInfo[] getDevices() {
035          
036         Vector devices = CaptureDeviceManager.getDeviceList(null);
037      
038         CaptureDeviceInfo[] info =new CaptureDeviceInfo[devices.size()];
039         for(int i = 0; i < devices.size(); i++) {
040             info[i] = (CaptureDeviceInfo) devices.get(i);
041         }
042         returninfo;
043     }
044     // 从已知设备中取所有视频设备的列表
045     privateCaptureDeviceInfo[] getVideoDevices() {
046          
047         CaptureDeviceInfo[] info = getDevices();
048          
049         System.out.println("设备数:"+info.length);
050          
051         CaptureDeviceInfo[] videoDevInfo;
052          
053         Vector vc =new Vector();
054          
055         for(int i = 0; i < info.length; i++) {
056              
057         // 取设备支持的格式,如果有一个是视频格式,则认为此设备为视频设备
058         Format[] fmt = info[i].getFormats();
059          
060         System.out.println("fmt.length="+fmt.length);
061          
062             for(int j = 0; j < fmt.length; j++) {
063                 if(fmt[j] instanceof VideoFormat) {
064                     System.out.println("fmt[j] "+fmt[j]);
065                     vc.add(info[i]);
066                 }
067                 break;
068             }
069         }
070      
071         videoDevInfo =new CaptureDeviceInfo[vc.size()];
072      
073         for(int i = 0; i < vc.size(); i++) {
074             videoDevInfo[i] = (CaptureDeviceInfo) vc.get(i);
075         }
076          
077         System.out.println("videoDevInfo "+videoDevInfo.length);
078          
079         returnvideoDevInfo;
080     }
081  
082     privateJPanel getJContentPane() {
083     if(jContentPane == null) {
084     BorderLayout borderLayout =new BorderLayout();
085     jContentPane =new JPanel();
086     jContentPane.setLayout(borderLayout);
087       
088     MediaLocator ml =null;
089     Player player =null;
090     try{
091     // 这里我只有一个视频设备,直接取第一个
092      // 取得当备的MediaLocator
093     ml = getVideoDevices()[0].getLocator();
094     // 用已经取得的MediaLocator得到一个Player
095     player = Manager.createRealizedPlayer(ml);
096     player.start();
097     // 取得Player的AWT Component
098     Component comp = player.getVisualComponent();
099     // 如果是音频设备这个方法将返回null,这里要再判断一次
100     if(comp != null) {
101     // 将Component加到窗体
102     jContentPane.add(comp, BorderLayout.EAST);
103     }
104     } catch (Exception e) {
105     e.printStackTrace();
106     }
107     }
108     returnjContentPane;
109     }
110     }
1  
1  
1  
1 运行效果如图所示.
1

<img src="http://pic002.cnblogs.com/img/cayden/201007/2010070117042292.jpg"alt="视频图片">











jmf启动摄像头拍照

(2011-07-23 15:20:53)
转载
标签:

it

分类:java

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.event.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

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 javax.swing.JScrollPane;
import javax.swing.JTextField;

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 GetPhoto extends JPanel implements ActionListener{

 private CaptureDeviceInfocaptureDeviceInfo=null;
 private MediaLocator mediaLocator=null;
 private static Player player=null;
 private ImagePanel imagePanel=null;
 private JButton jbok,jbcancel;
 private Buffer buffer=null;
 private VideoFormat videoFormat=null;
 private BufferToImage bufferToImage=null;
 private Image image=null;
 private JPanel jp=new JPanel();
 
 public GetPhoto()
 {
  //setLayout(newBorderLayout());
  this.setLayout(null);
  setSize(300,500);
  String str="vfw:Microsoft WDMImage Capture (Win32):0";
  captureDeviceInfo=CaptureDeviceManager.getDevice(str);
  mediaLocator=newMediaLocator("vfw://0");
  imagePanel=newImagePanel();
  jbok=newJButton("拍   照");
  jbcancel=newJButton("重   拍");
  jbok.addActionListener(this);
  jbcancel.addActionListener(this);
  
  try {
   player=Manager.createRealizedPlayer(mediaLocator);
   player.start();
   Componentcomp;
   if((comp=player.getVisualComponent())!=null)
   comp.setBounds(0,0, 240, 180);
   this.add(comp);
  } catch (NoPlayerExceptione)
  {
   e.printStackTrace();
  } catch (CannotRealizeExceptione)
  {
   e.printStackTrace();
  } catch (IOException e)
  {
   e.printStackTrace();
  }  
  jp.add(jbok);
  jp.add(jbcancel);
  jp.add(imagePanel);
  jp.setBounds(0, 180, 240,200);
  this.add(jp);

 }

 public void print(Graphics g)
 {
  super.print(g);
  g.setColor(newColor(255,0,0));
  g.drawLine(0, 0, 100,100);
 }
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==jbok)
  {
   FrameGrabbingControlfgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
   buffer=fgc.grabFrame();
   bufferToImage=newBufferToImage((VideoFormat)buffer.getFormat());
   image=bufferToImage.createImage(buffer);
   imagePanel.setImage(image);
   saveImage(image,"temp.jpg");
  }
  if(e.getSource()==jbcancel)
  {
   imagePanel.setImage(null);
  }
   
 }

 public static void saveImage(Imageimage,String path)
 {
   BufferedImagebi=newBufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
   Graphics2D g2= bi.createGraphics();
   g2.drawImage(image,null, null);
   FileOutputStreamfos=null;
   
   try {
    fos=newFileOutputStream(path);

   } catch(FileNotFoundException e)
   {
    e.printStackTrace();
   }
   JPEGImageEncoderje=JPEGCodec.createJPEGEncoder(fos);
   JPEGEncodeParamjp=je.getDefaultJPEGEncodeParam(bi);
   jp.setQuality(1f,false);
   je.setJPEGEncodeParam(jp);
   try {
    je.encode(bi);
    fos.close();
   } catch(ImageFormatException e)
   {
    e.printStackTrace();
   } catch(IOException e)
   {
    e.printStackTrace();
   }

 }
 public static File getImage()
 {
  File f=newFile("temp.jpg");
  return f;
  
 }
 
 class ImagePanel extends Panel
 {
   public Imagemyimg = null;

   publicImagePanel()
   {
    setLayout(null);
    setSize(240,180);
   }

   publicvoid setImage(Image img)
   {
    this.myimg= img;
    repaint();
   }

   publicvoid paint(Graphics g)
   {
    if(myimg != null)
    {
     g.drawImage(myimg,0, 0, this);
    }

   }
 
 }
 
 public static void main(String[] args)
 {
  JFrame f = newJFrame("FirstCapture");
  GetPhoto cf = newGetPhoto();

  f.addWindowListener(newWindowAdapter() {
   public voidwindowClosing(WindowEvent e) {
    player.close();
   System.exit(0);}});

  f.add("Center",cf);
  f.pack();
  f.setBounds(100,100,240,550);
  f.setVisible(true);
 }
 
}



你可能感兴趣的:(image,vector,Microsoft,null,buffer,import)