⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videomonitor.java

📁 j2me实现的移动机器人代码(Java实现)
💻 JAVA
字号:
package cie.mobile.video;

import name.lxm.robot.arch.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import sun.awt.image.codec.JPEGImageDecoderImpl;

public class VideoMonitor extends AbstractModule
{
	String[] port_names = {"image"};
	SimpleInPort img_port = null;
	Object lock = new Object();
	BufferedImage img = null;
	JFrame win = null;
	MyPanel display = null;

	public VideoMonitor()
	{
		win = new JFrame("Video");
		display = new MyPanel();
	}
	
	public void init(ModuleDoc doc) throws Exception
	{
		super.init(doc);
		img_port = new SimpleInPort(this, port_names[0]);
		img_port.registerListener(this);
		win.getContentPane().setLayout(new BorderLayout());
		win.getContentPane().add(display, BorderLayout.CENTER);
		win.pack();
		win.setResizable(false);
		win.setVisible(true);
	}

	public void run()
	{
		while(bRun)
		{
			synchronized(lock)
			{
				try{
					lock.wait();
				}catch(InterruptedException e){}
			}
			try{
				readImage();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public void readImage() throws Exception
	{
		Object o = img_port.getValue();
		byte[] data;
		if(!(o instanceof byte[]) )
		{
			System.out.println("Bad image data, drop it.");
			return;
		}
		data = (byte[]) o;
		if(data == null)
		{
			System.out.println("No image data available.");
			return;
		}
		ByteArrayInputStream bin = new ByteArrayInputStream(data);
		JPEGImageDecoderImpl jpeg = new JPEGImageDecoderImpl(bin);
		img = jpeg.decodeAsBufferedImage();
		display.repaint(img);
		bin.close();
	}
	
	public Port getPort(String port_name)
	{
		if(port_names[0].equals(port_name))
			return img_port;
		return null;
	}

	public void valueUpdated()
	{
		synchronized(lock)
		{
			lock.notifyAll();
		}
	}
}

class MyPanel extends JPanel
{
	BufferedImage img = null;

	public MyPanel()
	{
		setPreferredSize(new Dimension(160, 120));
	}
	
	public void paint(Graphics g)
	{
		//super.paint(g);
		int width = this.getWidth();
		int height = this.getHeight();
		
		if(img != null)
			g.drawImage(img, 0, 0, width, height, this);		
	}

	public void repaint(BufferedImage image)
	{
		img = image;
		repaint();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -