日期:2014-05-20  浏览次数:20613 次

求教一个java摄像头实时摄像程序。
求教一个java摄像头实时摄像程序,高分求教。要求,有相关的程序说明

------解决方案--------------------
用Java Media Framework
下载地址: http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/download.html
示范程序: http://java.sun.com/javase/technologies/desktop/media/jmf/reference/codesamples/index.html
------解决方案--------------------
网上找的,希望对你有用
Java code
package com.redtroy; 

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();