Bonjour
Je suis en train de realiser une applet qui affiche le flux video d'un camera IP
L'applet fonctionne parfaitement sous Eclipse 3.1 mais j'ai un probleme lors du lancement de cette applet dans un navigateur web : l'applet s'initialise bien, demarre bien, mais aucune image ne s'affiche
Pouvez vous m'aider?
Mon site tourne sur un serveur Tomcat 5.5
voila comment j'apelle mon applet sur mon site :
<applet code=MyApplet.class width=640 height=480>
</applet>
et voila le code de mon applet :
Code :import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
public class MyApplet extends JApplet {
private static final long serialVersionUID = 1L;
private Thread mythread = null;
MJPEGBeanA axPanel = null;
public void init()
{
// TODO Auto-generated method stub
super.init();
axPanel = new MJPEGBeanA();
this.getContentPane().add(axPanel);
this.setVisible(true);
}
public void start()
{
mythread = new Thread(axPanel);
mythread.start();
}
}
class MJPEGBeanA extends JComponent implements Runnable, ChangeListener{
private static final long serialVersionUID = 1L;
// URL Caméra Japon Tohoku
public String mjpgURL = "http://130.34.82.18/cgi-bin/video640x480.mjpg";
String username = "root";
String password = "root";
String base64authorization = null;
private Image image = null;
private boolean connected = false;
private boolean initCompleted = false;
HttpURLConnection huc = null;
Runnable updater;
/**
* Used to calculate an average FPS
*/
long[] frameTimes = new long[10];
MJPEGParserA parser;
/** Creates a new instance of AxisCamera */
public MJPEGBeanA() {
// only uwse authorization if all informations are available
if (username != null && password != null) {
base64authorization = encodeUsernameAndPasswordInBase64(username, password);
}
setForeground(Color.YELLOW);
setFont(new Font("Dialog", Font.BOLD, 12));
/**
* See the stateChanged() function
*/
updater = new Runnable() {
public void run() {
if (!initCompleted) {
initDisplay();
}
repaint();
for (int i = 0; i < frameTimes.length - 1; i++) {
frameTimes[i] = frameTimes[i + 1];
}
frameTimes[frameTimes.length - 1] = System.currentTimeMillis();
}
};
}
private String encodeUsernameAndPasswordInBase64(String usern, String psswd) {
//String s = usern + ":" + psswd;
String encs = ("cm9vdDpyb290" );
return "Basic " + encs;
}
public void connect() {
try {
URL u = new URL(mjpgURL);
huc = (HttpURLConnection) u.openConnection();
if (base64authorization != null) {
huc.setDoInput(true);
huc.setRequestProperty("Authorization", base64authorization);
huc.connect();
}
String boundary = "--myboundary";
String contentType = huc.getContentType();
Pattern pattern = Pattern.compile("boundary=(.*)$" );
Matcher matcher = pattern.matcher(contentType);
try {
matcher.find();
boundary = matcher.group(1);
} catch (Exception e) {
}
InputStream is = huc.getInputStream();
connected = true;
parser = new MJPEGParserA(is, boundary);
parser.addChangeListener(this);
} catch (IOException e) {
try {
huc.disconnect();
Thread.sleep(60);
} catch (InterruptedException ie) {
huc.disconnect();
}
connect();
} catch (Exception e) {
;
}
}
public void initDisplay() { // setup the display
if (image != null) {
Dimension imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
setPreferredSize(imageSize);
SwingUtilities.getWindowAncestor(this).pack();
initCompleted = true;
}
}
public void disconnect() {
try {
if (connected) {
parser.setCanceled(true);
connected = false;
}
} catch (Exception e) {
;
}
}
NumberFormat decFormat = DecimalFormat.getNumberInstance();
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, this);
}
long timeBetweenFrames = 0;
for (int i = 0; i < frameTimes.length - 1; i++) {
timeBetweenFrames += frameTimes[i + 1] - frameTimes[i];
}
timeBetweenFrames /= frameTimes.length - 1;
g.setColor(getForeground());
g.setFont(getFont());
if (timeBetweenFrames > 0) {
String fps = decFormat.format(1000.0 / timeBetweenFrames) + " fps";
g.drawString(fps, 0, g.getFontMetrics().getHeight());
}
}
public void run() {
connect();
try {
parser.parse();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stateChanged(ChangeEvent e) {
byte[] segment = parser.getSegment();
if (segment.length > 0) {
try {
image = ImageIO.read(new ByteArrayInputStream(segment));
EventQueue.invokeLater(updater);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
MJPEGBeanA axPanel = new MJPEGBeanA();
new Thread(axPanel).start();
jframe.getContentPane().add(axPanel);
jframe.pack();
jframe.setVisible(true);
}
}
class MJPEGParserA {
private static final byte[] JPEG_START = new byte[] { (byte) 0xFF, (byte) 0xD8 };
private static final int INITIAL_BUFFER_SIZE = 4096;
InputStream in;
byte[] boundary;
byte[] segment;
byte[] buf;
int cur, len;
boolean canceled = false;
protected EventListenerList listenerList = new EventListenerList();
public boolean isCanceled() {
return canceled;
}
public void setCanceled(boolean canceled) {
this.canceled = canceled;
if (canceled) {
try {
in.close();
System.exit(0);
} catch (IOException e) {
}
}
}
public MJPEGParserA(InputStream in, String boundary) {
this.in = in;
this.boundary = boundary.getBytes();
buf = new byte[INITIAL_BUFFER_SIZE];
cur = 0;
len = INITIAL_BUFFER_SIZE;
}
public void parse() throws IOException {
int b;
while ((b = in.read()) != -1 && !canceled) {
append(b);
if (checkBoundary()) {
processSegment();
cur = 0;
}
}
}
/**
* Processes the current byte buffer. Ignores the last len(BOUNDARY) bytes in the buffer. Searches through the buffer
* for the start of a JPEG. If a JPEG is found, the bytes comprising the JPEG are copied into the
* <code>segment</code> field. If no JPEG is found, nothing is done.
*
*/
protected void processSegment() {
boolean found = false;
int i;
for (i = 0; i < cur - JPEG_START.length; i++) {
if (segmentsEqual(buf, i, JPEG_START, 0, JPEG_START.length)) {
found = true;
break;
}
}
if (found) {
int segLength = cur - boundary.length - i;
segment = new byte[segLength];
System.arraycopy(buf, i, segment, 0, segLength);
fireChange();
}
}
public byte[] getSegment() {
return segment;
}
protected boolean segmentsEqual(byte[] b1, int b1Start, byte[] b2, int b2Start, int len) {
if (b1Start < 0 || b2Start < 0 || b1Start + len > b1.length || b2Start + len > b2.length) {
return false;
} else {
for (int i = 0; i < len; i++) {
if (b1[b1Start + i] != b2[b2Start + i]) {
return false;
}
}
return true;
}
}
protected boolean checkBoundary() {
return segmentsEqual(buf, cur - boundary.length, boundary, 0, boundary.length);
}
public int getBufferSize() {
return len;
}
protected void append(int i) {
if (cur >= len) {
byte[] newBuf = new byte[len * 2];
System.arraycopy(buf, 0, newBuf, 0, len);
buf = newBuf;
len = len * 2;
}
buf[cur++] = (byte) i;
}
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
public ChangeListener[] getChangeListeners() {
return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
}
protected void fireChange() {
Object[] listeners = listenerList.getListenerList();
ChangeEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
if (e == null)
e = new ChangeEvent(this);
((ChangeListener) listeners[i + 1]).stateChanged(e);
}
}
}
}