资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
利用JMF进行摄像头拍照:) 疯狂代码 http:/CrazyCoder.cn/ :http:/CrazyCoder.cn/Java/Article6991.html 我把分为两种有趣和无趣最近做了几个有趣项目其中个应当就算是摄像头拍照了:)用于现场拍照生成照片主要 用到Java Media Framework(JMF) 首先到SUN下载最新JMF然后安装java.sun.com/products/java- media/jmf/index.jsp“http:/java.sun.com/products/java-media/jmf/index.jsp 然后说下需求 1 用摄像头拍照 2 在文本框输入文件名 3 按下拍照按钮获取摄像头内图像 4 在拍下照片上有红框截取固定大小照片 5 保存为本地图像为jpg格式不得压缩画质 技术关键相信也是大家最感兴趣部分也就是如何让个摄像头工作并拍下张照片了 利用JMF代码很简单: /利用这 3个类分别获取摄像头驱动和获取摄像头内图像流获取到图像流是个SwingComponent组件类 public Player player = null; private CaptureDeviceInfo di = null; private MediaLocator ml = null; /文档中提供驱动写法为何这么写我也不知:) String str1 = “vfw:Logitech USB Video Camera:0“; String str2 = “vfw:Microsoft WDM Image Capture (Win32):0“; di = CaptureDeviceManager.getDevice(str2); ml = di.getLocator; try player = Manager.createRealizedPlayer(ml); player.start; Component comp; (comp = player.getVisualComponent) != null) add(comp, BorderLayout.NORTH); catch (Exception e) e.prStackTrace; 接下来就是点击拍照获取摄像头内当前图像 代码也是很简单: private JButton capture; private Buffer buf = null; private BufferToImage btoi = null; private ImagePanel imgpanel = null; private Image img = null; private ImagePanel imgpanel = null; JComponent c = (JComponent) e.getSource; (c capture)/如果按下是拍照按钮 FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl( “javax.media.control.FrameGrabbingControl“); buf = fgc.grabFrame; / 获取当前祯并存入 Buffer类 btoi = BufferToImage(VideoFormat) buf.getFormat); img = btoi.createImage(buf); / show the image imgpanel.Image(img); 保存图像就不多说了以下为举例代码 BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight); Graphics2D g2 = bi.createGraphics; g2.drawImage(img, null, null); FileOutputStream out = null; try out = FileOutputStream(s); catch (java.io.FileNotFoundException io) .out.prln(“File Not Found“); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.Quality( 1f, false);/不压缩图像 encoder.JPEGEncodeParam(param); try encoder.encode(bi); out.close; catch (java.io.IOException io) .out.prln(“IOException“); 已经申请将JWebCam建立为个开源项目放到GRO大家发挥自己想象力加入自己代码吧比如拍摄视频添加图像 处理功能等等如果有改很Cool记得发给我看看:) 2009-2-12 5:05:07 疯狂代码 http:/CrazyCoder.cn/
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号