jmf 添加"定制器件"问题,请那位仁兄帮看看,Thanks:)

z3951188 2006-04-24 08:55:02
我用jmf控制射相头拍照,并保存到指定位置,我想让程序开始后,每隔1000毫秒自动拍照一次并保存,我加了个定制器件,可运行有问题,请哪位仁兄能抽出时间静下心来帮忙解决,感谢非常!

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.*;

//
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();
Timer t=new Timer(1000);
TimerAction listener=new TimerAction();
t.addTimerListener(listener);
boolean proportion = true;
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
JButton jButton1 = new JButton();
Component component1;
JLabel jLabel1 = new JLabel();
//
public Camera() {
super("我的摄像机");
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("d:\\temp.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
encoder.encode(buffImg);
tempout.close();

}
catch (Exception e) {
e.printStackTrace();
}
return tk.createImage("d:\\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();
}

jButton1.setBounds(new Rectangle(290, 28, 100, 25));
jButton1.setText("开始");
jButton1.addActionListener(new Camera_jButton1_actionAdapter(this));
this.getContentPane().setLayout(null);
component1.setBounds(new Rectangle(27, 23, 243, 235));
jLabel1.setIconTextGap(4);
jLabel1.setText("空");
jLabel1.setVerticalTextPosition(SwingConstants.CENTER);

jLabel1.setBounds(new Rectangle(293, 139, 80, 95));

this.getContentPane().add(jButton1, null);

this.getContentPane().add(jLabel1, null);
this.setSize(400, 300);
this.setVisible(true);
}
public class TimerAction implements TimerListener
{
public void timeElapsed(TimerEvent event)
{
iicon = new ImageIcon();
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl(
"javax.media.control.FrameGrabbingControl");
buffer = fgc.grabFrame();
b2i = new BufferToImage( (VideoFormat) buffer.getFormat());
image = b2i.createImage(buffer);
iicon = new ImageIcon();
iicon.setImage(this.resize(85, 95, image, true));
jLabel1.setIcon(iicon);

}
}
}
class TimerEvent extends AWTEvent
{
public TimerEvent(Timer t) {super(t,TIMER_EVENT);}
public static final int TIMER_EVENT
=AWTEvent.RESERVED_ID_MAX+5555;
}

interface TimerListener extends EventListener
{
public void timeElapsed(TimerEvent event);
}

class Timer extends Component implements Runnable
{
public Timer(int i)
{
listenerList=new EventListenerList();
interval=i;
Thread t=new Thread(this);
t.start();
}

public void addTimerListener(TimerListener listener)
{
listenerList.add(TimerListener.class,listener);
}

public void removeTimerListener(TimerListener listener)
{
listenerList.add(TimerListener.class,listener);
}

public void run()
{
while (true)
{
try{Thread.sleep(interval);}
catch(InterruptedException e){}
TimerEvent event=new TimerEvent(this);
EventQueue queue
=Toolkit.getDefaultToolkit().getSystemEventQueue();
queue.postEvent(event);
}
}

public void processEvent(AWTEvent event)
{
if(event instanceof TimerEvent)
{
EventListener[] listener=listenerList.getListeners(TimerListener.class);
for(int i=0;i<listeners.length;i++)
((TimerListener)listeners[i]).timeElapsed((TimerEvent)event);
}
else
super.processEvent(event);
}
//
class Camera_jButton1_actionAdapter
implements java.awt.event.ActionListener {
Camera adaptee;

Camera_jButton1_actionAdapter(Camera adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
private int interval;
private EventListenerList listeners;
}
...全文
115 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,635

社区成员

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

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