使用jmf进行拍照

1 篇文章 0 订阅

import java.awt.EventQueue;

import javax.media.Buffer; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.Player; import javax.media.control.FrameGrabbingControl; import javax.media.format.VideoFormat; import javax.media.protocol.DataSource; import javax.media.util.BufferToImage; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Component; import java.awt.FlowLayout; import java.awt.Graphics2D; import java.awt.Image; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.SwingConstants; import java.awt.Font; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; //下面三个包要注意下 import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.media.util.BufferToBufferedImage;

//请自行下载jmf.jar /** * @author 后飞IT * @time 2013-06-24 01:37 * 功能:多次拍照生成图片 * 打开图片保存目录 */ public class Take_pic { private JFrame frame; Player player; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Take_pic window = new Take_pic(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Take_pic() { initialize(); } /** * Initialize the contents of the frame. * @throws */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 472, 418); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); final JPanel panel = new JPanel(); FlowLayout flowLayout = (FlowLayout) panel.getLayout(); flowLayout.setVgap(0); flowLayout.setHgap(0); panel.setBounds(69, 59, 326, 232); frame.getContentPane().add(panel); JLabel label = new JLabel("\u62CD\u7167\u529F\u80FD"); label.setFont(new Font("楷体", Font.PLAIN, 20)); label.setHorizontalAlignment(SwingConstants.CENTER); label.setBounds(136, 21, 176, 28); frame.getContentPane().add(label); JButton button = new JButton("\u62CD\u7167"); button.setFont(new Font("楷体", Font.PLAIN, 18)); button.setBounds(69, 323, 93, 23); frame.getContentPane().add(button); //初始化的过程,本地获取的视屏信息需要一直播放中 player=showVedio(panel); JButton button_1 = new JButton("\u7EE7\u7EED"); button_1.setFont(new Font("楷体", Font.PLAIN, 18)); button_1.setBounds(302, 325, 93, 23); frame.getContentPane().add(button_1); JButton button_2 = new JButton("浏览"); button_2.setFont(new Font("楷体", Font.PLAIN, 18)); button_2.setBounds(187, 325, 93, 23); frame.getContentPane().add(button_2); //打开图片保存目录 button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Runtime.getRuntime().exec("explorer.exe D:\\temp"); } catch (IOException e1) { e1.printStackTrace(); } } }); //重新开始拍照 button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //重新开始播放器的开始 player.start(); } }); //拍照按钮激活 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //获取视频流 FrameGrabbingControl fgc=(FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); Buffer buffer=fgc.grabFrame(); try { BufferToImage bti =new BufferToImage((VideoFormat) buffer.getFormat()); Image image=bti.createImage(buffer); JLabel label=new JLabel(); label.setBounds(69, 59, 326, 232); ImageIcon imageicon=new ImageIcon(image); //要先把播放器停掉,才能将后边的label设置上去 player.stop(); label.setIcon(imageicon); panel.add(label); //保存图片设置 BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(image, null, null); Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String name=sdf.format(date); //加入不存这个目录的话,生成一个这样的目录 if(!new File("D:\\temp").isDirectory()){ new File("D:\\temp") .mkdir(); } FileOutputStream fos = new FileOutputStream("D:\\temp\\"+name+".jpg"); JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(fos); je.encode(bi); fos.flush(); fos.close(); } catch (Exception e1) { e1.printStackTrace(); } } }); } /**将视屏设备获取的视屏在Jpanel上显示出来*/ private Player showVedio( JPanel panel) { Component com; Player player=null; try { //vfw:Logitech USB Video Camera:0
			//在win7上利用其提供的搜索设备是查找不到设备的!所以可以直接写上媒体定位器的位置
			MediaLocator loator=new MediaLocator("vfw://0");
			Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));//渲染画面,是画面效果更好
			DataSource ds=Manager.createDataSource(loator);//通过媒体定位器创建一个数据源
			player=Manager.createRealizedPlayer(ds);//通过数据源创建一个处于工作状态的player
			player.start();//开始工作,即是显示在JPANEL上
			com =player.getVisualComponent();//将player的可视化组件添加到com上
			com.setBounds(69, 59, 326, 232);
			panel.add(com);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(player);
		return player;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值