日期:2014-05-17 浏览次数:20697 次
import javax.media.Player; import javax.media.CaptureDeviceInfo; import javax.media.MediaLocator; import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.media.control.FrameGrabbingControl; import javax.media.Buffer; import javax.media.util.BufferToImage; import javax.media.format.VideoFormat; import java.io.*; import com.sun.image.codec.jpeg.*; import javax.media.CaptureDeviceManager; import javax.media.Manager; import java.net.*; import java.applet.*; //import javax.swing.im /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ public class Camera extends JFrame { private static Player player = null; private CaptureDeviceInfo device = null; private MediaLocator locator = null; private Buffer buffer = null; private BufferToImage b2i = null; private Image image; private ImageIcon iicon = new ImageIcon(); boolean proportion = true; String str1 = "vfw:Logitech USB Video Camera:0"; String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); Component component1; JLabel jLabel1 = new JLabel(); public Camera() { super("troy的摄像机(无保存功能)"); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public Image resize(int width, int height, Image source, boolean flag) { this.proportion = flag; int new_w; int new_h; Toolkit tk = Toolkit.getDefaultToolkit(); Applet app = new Applet(); MediaTracker mt = new MediaTracker(app); Image img = source; try { mt.addImage(img, 0); mt.waitForID(0); } catch (Exception e) { e.printStackTrace(); } if (img.getWidth(null) == -1) { System.out.println(" can't read,retry!" + "<BR>"); return null; } else { if (this.proportion == true) { //判断是否是等比缩放. //为等比缩放计算输出的图片宽度及高度 double rate1 = ( (double) img.getWidth(null)) / (double) width + 0.1; double rate2 = ( (double) img.getHeight(null)) / (double) height + 0.1; double rate = rate1 > rate2 ? rate1 : rate2; new_w = (int) ( ( (double) img.getWidth(null)) / rate); new_h = (int) ( ( (double) img.getHeight(null)) / rate); } else { new_w = width; //输出的图片宽度 new_h = height; //输出的图片高度 } } BufferedImage buffImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB); Graphics g = buffImg.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, new_w, new_h); g.drawImage(img, 0, 0, new_w, new_h, null); g.dispose(); try { OutputStream tempout = new FileOutputStream("C:\\temp.jpg"); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout); encoder.encode(buffImg); tempout.close(); } catch (Exception e) { e.printStackTrace(); } return tk.createImage("C:\\temp.jpg"); } public static void main(String[] args) { Camera camera1 = new Camera(); } private void jbInit() throws Exception { component1 = Box.createGlue(); //=====================初始化设备===================// component1.addNotify(); device = CaptureDeviceManager.getDevice(str2); locator = device.getLocator(); try { player = Manager.createRealizedPlayer(locator); player.start(); if ( (component1 = player.getVisualComponent()) != null) { this.getContentPane().add(component1, null); } } catch (Exception e) { e.printStackTrace();