在浏览器上运行Java小程序 [英] Run a java applet on browser

查看:143
本文介绍了在浏览器上运行Java小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我编写了一个Java Applet,用于显示ipcamera的图像.它在嵌入Eclipse的Applet Viewer上运行良好,但在浏览器上却无法运行.如果您知道,请帮助我.谢谢分配

这是源代码:

Hi every one,
I wrote a Java Applet for displaying image from ipcamera. It work well on Applet Viewer that embed on Eclipse but doesn''t work on browser. Please help me if you known. Thanks allots

Here is the source code:

import java.applet.Applet;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

/**
*
* @author David E. Mireles, Ph.D.
*/
public class LiveDemo extends Applet implements Runnable {
	/**
	* 
	*/
	private static final long serialVersionUID = 1L;
	public boolean useMJPGStream = false;//true;
	public String jpgURL="http://10.0.0.81/axis-cgi/jpg/image.cgi?resolution=1024x768";
	public String mjpgURL="http://10.0.0.81/axis-cgi/jpg/image.cgi?resolution=1024x768";
	DataInputStream dis;
	private Image image = null;
	public Dimension imageSize = null;
	public boolean connected = false;
	private boolean initCompleted = false;
	HttpURLConnection huc = null;
	Component parent;
	
	/** Creates a new instance of Ax52Camera */
	public void init()
	{
		new Thread(this).start();
	}
	
	public void connect(){
		try{
			URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
			huc = (HttpURLConnection)u.openConnection();			
			
			System.out.println(huc.getContentType());
			InputStream is = huc.getInputStream();
			System.out.println(is.toString());
			connected = true;
			BufferedInputStream bis = new BufferedInputStream(is);
			dis= new DataInputStream(bis);
			if (!initCompleted) initDisplay();
		}catch(IOException e){ //incase no connection exists wait and try again, instead of printing the error
			try{
				huc.disconnect();
				Thread.sleep(33);
			}catch(InterruptedException ie){huc.disconnect();connect();}
			connect();
		}catch(Exception e){;}
	}
	
	public void initDisplay(){ //setup the display
		if (useMJPGStream)readMJPGStream();
		else {readJPG();disconnect();}
		imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
		setPreferredSize(imageSize);
		this.setSize(imageSize);
		this.validate();
		initCompleted = true;
	}
	
	public void disconnect(){
		try{
			if(connected){
				dis.close();
				connected = false;
			}
		}catch(Exception e){;}
	}
	
	public void paint(Graphics g) { //used to set the image on the panel					
		if (image != null)
			g.drawImage(image, 0, 0, this);
	}
	
	public void readStream(){ //the basic method to continuously read the stream
		try{
			if (useMJPGStream){
				while(true){
					readMJPGStream();
					this.repaint();
				}
			}
			else{
				while(true){
					connect();
					readJPG();
					this.repaint();
					disconnect();
				}
			}
	
		}catch(Exception e){;}
	}
	
	
	public void readMJPGStream(){ //preprocess the mjpg stream to remove the mjpg encapsulation
		readLine(3,dis); //discard the first 3 lines
		readJPG();
		readLine(2,dis); //discard the last two lines
	}
	
	public void readJPG(){ //read the embedded jpeg image
		try{
			JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
			image = decoder.decodeAsBufferedImage();
		}catch(Exception e){e.printStackTrace();disconnect();}
	}
	
	public void readLine(int n, DataInputStream dis){ //used to strip out the header lines
		for (int i=0; i<n;i++){
		readLine(dis);
		}
	}
	public void readLine(DataInputStream dis){
		try{
			boolean end = false;
			String lineEnd = "\n"; //assumes that the end of the line is marked with this
			byte[] lineEndBytes = lineEnd.getBytes();
			byte[] byteBuf = new byte[lineEndBytes.length];
			
			while(!end){
				dis.read(byteBuf,0,lineEndBytes.length);
				String t = new String(byteBuf);
				//System.out.print(t); //uncomment if you want to see what the lines actually look like
				if(t.equals(lineEnd)) end=true;
			}
		}catch(Exception e){e.printStackTrace();}
	
	
	}
	public void run() {
		connect();
		readStream();
	}
	
}

推荐答案

您需要在浏览器中启用Java:
http://www.java.com/en/download/help/enable_browser.xml [ ^ ]

并将您的applet嵌入到html页面中:
http://www.java-forums.org/java- applets/11851-first-applet-not-running-browsers.html [
You need to enable java in the browser:
http://www.java.com/en/download/help/enable_browser.xml[^]

And embed your applet into a html page:
http://www.java-forums.org/java-applets/11851-first-applet-not-running-browsers.html[^]

Good luck!


这篇关于在浏览器上运行Java小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆