备注学院

LuLu

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  5 随笔 :: 50 文章 :: 16 评论 :: 0 Trackbacks

首先到SUN下载最新的JMF,然后安装。http://java.sun.com/products/java-media/jmf/index.jsp

然后,说一下需求

1. 用摄像头拍照

2. 在文本框输入文件名

3. 按下拍照按钮,获取摄像头内的图像

4. 在拍下的照片上有一红框截取固定大小的照片。

5. 保存为本地图像为jpg格式,不得压缩画质

技术关键,相信也是大家最感兴趣的部分也就是如何让一个摄像头工作,并拍下一张照片了。

利用JMF,代码很简单:

//利用这三个类分别获取摄像头驱动,和获取摄像头内的图像流,获取到的图像流是一个Swing的Component组件类

 

 1public static Player player = null;
 2              private CaptureDeviceInfo di = null;
 3              private MediaLocator ml = null;
 4              String str1 = "vfw:Logitech USB Video Camera:0";
 5              String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
 6              di = CaptureDeviceManager.getDevice(str2);
 7              ml = di.getLocator();
 8              try
 9              {
10              player = Manager.createRealizedPlayer(ml);
11              player.start();
12              Component comp;
13              if ((comp = player.getVisualComponent()) != null)
14              {
15              add(comp, BorderLayout.NORTH);
16              }

17              }

18              catch (Exception e)
19              {
20              e.printStackTrace();
21              }


接下来就是点击拍照,获取摄像头内的当前图像。

代码也是很简单:

 1private JButton capture;
 2              private Buffer buf = null;
 3              private BufferToImage btoi = null;
 4              private ImagePanel imgpanel = null;
 5              private Image img = null;
 6              private ImagePanel imgpanel = null;
 7              JComponent c = (JComponent) e.getSource();
 8              if (c == capture)//如果按下的是拍照按钮 http://www.5a520.cn
 9              {
10              FrameGrabbingControl fgc =(FrameGrabbingControl)player.getControl
11            ("javax.media.control.FrameGrabbingControl");
12              buf = fgc.grabFrame(); // 获取当前祯并存入Buffer类 http://www.bt285.cn
13              btoi = new BufferToImage((VideoFormat) buf.getFormat());
14              img = btoi.createImage(buf); // show the image
15              imgpanel.setImage(img);
16              }

保存图像的就不多说了,以下为示例代码

 1BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
 2              Graphics2D g2 = bi.createGraphics();
 3              g2.drawImage(img, nullnull);
 4              FileOutputStream out = null;
 5              try
 6              {
 7              out = new FileOutputStream(s);
 8              }

 9              catch (java.io.FileNotFoundException io)
10              {
11              System.out.println("File Not Found");
12              }

13              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
14              JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
15              param.setQuality(1f, false);//不压缩图像 http://www.bt285.cn
16              encoder.setJPEGEncodeParam(param);
17              try
18              {
19              encoder.encode(bi);
20              out.close();
21              }

22              catch (java.io.IOException io)
23              {
24              System.out.println("IOException");
25              }

From:http://www.blogjava.net/fundei/archive/2009/05/21/271147.html
posted on 2009-05-27 09:40 smildlzj 阅读(139) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: