JMF问题,悬赏100

z3951188 2006-04-18 06:40:26
摄像头拍照程序,如何使这个图象显示连贯,并可以保存,谁可以帮我把缺少的部分加上去

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;



public class FrameGrab1
{

public static void main(String[] args) throws Exception
{


MediaFrame M = new MediaFrame();
M.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
M.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

M.show();

}
}
class MediaFrame extends JFrame
{
public MediaFrame()
{
this.setSize(400,300);
Container c = this.getContentPane();
MediaPanel p = new MediaPanel();
c.add(p,BorderLayout.CENTER);
JPanel buttonpanel = new JPanel();
JButton okbutton = new JButton("启动设备");
JButton imagebutton = new JButton("拍照");
JButton closebutton = new JButton("停止设备");

okbutton.addActionListener(new okaction());
imagebutton.addActionListener(new imageaction());
closebutton.addActionListener(new closeaction());


buttonpanel.add(okbutton);
buttonpanel.add(imagebutton);
buttonpanel.add(closebutton);
c.add(buttonpanel,BorderLayout.SOUTH);
}


class MediaPanel extends JPanel
{
public MediaPanel()
{


deviceInfo = CaptureDeviceManager.getDevice
("vfw:Microsoft WDM Image Capture (Win32):0");
try
{

}
catch(Exception e)
{
e.printStackTrace();
}

}



public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
if (img != null) drawimage(g2);

}
}
public void windowClosing ( WindowEvent event ) {
this.setVisible ( false );
}

public void drawimage(Graphics2D g2)
{
g2.drawImage(img,null,null);
}

public class okaction implements ActionListener
{

public void actionPerformed(ActionEvent envent)
{

try
{
player = Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class imageaction implements ActionListener
{

public void actionPerformed(ActionEvent envent)
{
setImage(img);
}
}
public class closeaction implements ActionListener
{

public void actionPerformed(ActionEvent envent)
{
try
{
player.close();
player.deallocate();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

private CaptureDeviceInfo deviceInfo = null;
private Player player = null;
private Image img = null;
private Buffer buf = null;
private BufferToImage btoi = null;
}
...全文
119 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuij7718 2006-05-11
  • 打赏
  • 举报
回复
呵呵,现在好像JMF的应用越来越多了,帮顶
zclgod 2006-05-11
  • 打赏
  • 举报
回复
帮顶
jesse8013 2006-05-11
  • 打赏
  • 举报
回复
仔细看着
Player pl = null;
try {

CaptureDeviceInfo df=CaptureDeviceManager.getDevice
("vfw:Microsoft WDM Image Capture (Win32):0");
pl=Manager.createRealizedPlayer(df.getLocator());
pl.start();
Thread t=new Thread();
t.sleep(1000);
} catch (Exception e) {

}
FrameGrabbingControl fgc = (FrameGrabbingControl) pl.getControl(
"javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
//this.repaint();

BufferedImage bi = (BufferedImage) createImage(200, 200);
bi.getGraphics().drawImage(img,0,0,200,200,null);
JFileChooser chooser=new JFileChooser();
if (chooser.showSaveDialog(this) !=
JFileChooser.APPROVE_OPTION) {
return;
}
File file = chooser.getSelectedFile();

File f = new File(file.getAbsolutePath() + ".jpg");
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
} catch (java.io.FileNotFoundException io) {
System.out.println("File Not Found");
}

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
out.close();
pl.close();
initCapture();
} catch (java.io.IOException io) {
System.out.println("IOException");
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧