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 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
 
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);
   }
  }
 }
}