import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class GetPhoto extends JPanel implements ActionListener{
private CaptureDeviceInfocaptureDeviceInfo=null;
private MediaLocator mediaLocator=null;
private static Player player=null;
private ImagePanel imagePanel=null;
private JButton jbok,jbcancel;
private Buffer buffer=null;
private VideoFormat videoFormat=null;
private BufferToImage bufferToImage=null;
private Image image=null;
private JPanel jp=new JPanel();
public GetPhoto()
{
//setLayout(newBorderLayout());
this.setLayout(null);
setSize(300,500);
String str="vfw:Microsoft WDMImage Capture (Win32):0";
captureDeviceInfo=CaptureDeviceManager.getDevice(str);
mediaLocator=newMediaLocator("vfw://0");
imagePanel=newImagePanel();
jbok=newJButton("拍 照");
jbcancel=newJButton("重 拍");
jbok.addActionListener(this);
jbcancel.addActionListener(this);
try {
player=Manager.createRealizedPlayer(mediaLocator);
player.start();
Componentcomp;
if((comp=player.getVisualComponent())!=null)
comp.setBounds(0,0, 240, 180);
this.add(comp);
} catch (NoPlayerExceptione)
{
e.printStackTrace();
} catch (CannotRealizeExceptione)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
jp.add(jbok);
jp.add(jbcancel);
jp.add(imagePanel);
jp.setBounds(0, 180, 240,200);
this.add(jp);
}
public void print(Graphics g)
{
super.print(g);
g.setColor(newColor(255,0,0));
g.drawLine(0, 0, 100,100);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbok)
{
FrameGrabbingControlfgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
buffer=fgc.grabFrame();
bufferToImage=newBufferToImage((VideoFormat)buffer.getFormat());
image=bufferToImage.createImage(buffer);
imagePanel.setImage(image);
saveImage(image,"temp.jpg");
}
if(e.getSource()==jbcancel)
{
imagePanel.setImage(null);
}
}
public static void saveImage(Imageimage,String path)
{
BufferedImagebi=newBufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2= bi.createGraphics();
g2.drawImage(image,null, null);
FileOutputStreamfos=null;
try {
fos=newFileOutputStream(path);
} catch(FileNotFoundException e)
{
e.printStackTrace();
}
JPEGImageEncoderje=JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParamjp=je.getDefaultJPEGEncodeParam(bi);
jp.setQuality(1f,false);
je.setJPEGEncodeParam(jp);
try {
je.encode(bi);
fos.close();
} catch(ImageFormatException e)
{
e.printStackTrace();
} catch(IOException e)
{
e.printStackTrace();
}
}
public static File getImage()
{
File f=newFile("temp.jpg");
return f;
}
class ImagePanel extends Panel
{
public Imagemyimg = null;
publicImagePanel()
{
setLayout(null);
setSize(240,180);
}
publicvoid setImage(Image img)
{
this.myimg= img;
repaint();
}
publicvoid paint(Graphics g)
{
if(myimg != null)
{
g.drawImage(myimg,0, 0, this);
}
}
}
public static void main(String[] args)
{
JFrame f = newJFrame("FirstCapture");
GetPhoto cf = newGetPhoto();
f.addWindowListener(newWindowAdapter() {
public voidwindowClosing(WindowEvent e) {
player.close();
System.exit(0);}});
f.add("Center",cf);
f.pack();
f.setBounds(100,100,240,550);
f.setVisible(true);
}
}