package webcam;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
public class MJPG extends JPanel implements Runnable
{
public boolean useMJPGStream = true;
public boolean connected = false;
private boolean initCompleted = false;
public String jpgURL;
public String mjpgURL;
private String username;
private String password;
private String base64authorization = null;
private Image image = null;
public Dimension imageSize = null;
DataInputStream dis;
HttpURLConnection huc = null;
public MJPG (String Daten[])
{
mjpgURL = "http://" + Daten[0] + "/videostream.cgi";
jpgURL = "http://" + Daten[0] + "/videostream.cgi";
username = Daten[4];
password = Daten[1];
if(username == null || password == null)
{
System.out.println("Kein Benutzer angegeben!");
}
else
{
base64authorization = this.encodeUsernameAndPasswordInBase64(username, password);
}
}
private String encodeUsernameAndPasswordInBase64(String usern, String psswd)
{
String s = usern + ":" + psswd;
String encs = new sun.misc.BASE64Encoder().encode(s.getBytes());
return "Basic " + encs;
}
public void connect()
{
try
{
URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
huc = (HttpURLConnection) u.openConnection();
if(base64authorization != null)
{
huc.setDoInput(true);
huc.setRequestProperty("Authorization",base64authorization);
huc.connect();
}
InputStream is = huc.getInputStream();
connected = true;
BufferedInputStream bis = new BufferedInputStream(is);
dis = new DataInputStream(bis);
if (!initCompleted)
{
initDisplay();
}
}
catch(IOException e)
{
try
{
huc.disconnect();
Thread.sleep(60);
}
catch(InterruptedException ie)
{
huc.disconnect();
connect();
}
connect();
}
catch(Exception e)
{
}
}
public void initDisplay()
{
if (useMJPGStream)
{
readMJPGStream();
}
else
{
readJPG();
disconnect();
}
imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
setPreferredSize(imageSize);
initCompleted = true;
}
public void disconnect()
{
try
{
if(connected)
{
dis.close();
connected = false;
}
}
catch(Exception e)
{
}
}
public void paint(Graphics g)
{
if (image != null)
{
g.drawImage(image, 0, 0, this);
}
}
public void readStream()
{
try
{
if (useMJPGStream)
{
while(true)
{
readMJPGStream();
repaint();
}
}
else
{
while(true)
{
connect();
readJPG();
repaint();
disconnect();
}
}
}
catch(Exception e)
{
}
}
public void readMJPGStream()
{
readLine(4,dis);
readJPG();
readLine(1,dis);
}
public void readJPG()
{
try
{
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
image = decoder.decodeAsBufferedImage();
}
catch(Exception e)
{
e.printStackTrace();
disconnect();
}
}
public void readLine(int n, DataInputStream dis)
{
for (int i=0; i<n; i++)
{
readLine(dis);
}
}
public void readLine(DataInputStream dis)
{
try
{
boolean end = false;
String lineEnd = "\n";
byte[] lineEndBytes = lineEnd.getBytes();
byte[] byteBuf = new byte[lineEndBytes.length];
while(!end)
{
dis.read(byteBuf,0,lineEndBytes.length);
String t = new String(byteBuf);
if(t.equals(lineEnd))
{
end = true;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
connect();
readStream();
}
public Image getImage()
{
return image;
}
}