摄像头拍照程序,如何使这个图象显示连贯,并可以保存,谁可以帮我把缺少的部分加上去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;
}
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;
}
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");
}