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

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

开始代码

用applet比较快

001package com.cayden.video;
002 
003import java.awt.BorderLayout;
004import java.awt.Choice;
005import java.awt.Component;
006import java.util.Vector;
007 
008import javax.media.CaptureDeviceInfo;
009import javax.media.CaptureDeviceManager;
010import javax.media.Format;
011import javax.media.Manager;
012import javax.media.MediaLocator;
013import javax.media.Player;
014import javax.media.format.VideoFormat;
015import javax.swing.JApplet;
016import javax.swing.JPanel;
017 
018public 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);
 }
 
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值