1

我正在运行以下代码以从 ip 摄像机捕获实时视频流。如果我使用 Jframe 效果很好,出于某些要求,我需要将它包装在小程序中但它不起作用,任何人都可以帮忙..

private static final long serialVersionUID = 1L;
public boolean useMJPGStream = false;//true;
public String jpgURL="http://192.168.x.x:80/video.cgi/jpg/image.cgi?resolution=640×480";
public String mjpgURL="http://192.168.x.x:80/video.cgi/mjpg/video.cgi?resolution=640×480";
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();
}

}

我收到以下错误

load: class com.javaprac.AxisCamera1.class not found.
java.lang.ClassNotFoundException: com.javaprac.AxisCamera1.class
    at sun.applet.AppletClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.applet.AppletClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.applet.AppletClassLoader.loadCode(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

0

我能够运行这段代码......在applet的init方法中我调用了类的对象和这样的线程......

public void init() {
        ViewCamera axPanel = new ViewCamera(rootPane);
        axPanel.setOpaque(true);
        new Thread(axPanel).start();
        getContentPane().add(axPanel);

        // Execute a job on the event-dispatching thread; creating this applet's
        // GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {

                    createGUI();

                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }

我也在我的网页中嵌入了这个小程序....

于 2012-07-10T05:29:48.757 回答