I tried to "continuosly" grab and display a jpg image from http address. But my code below does not work
Anyone know how to fix this ?
Thanks
package jpgmidlet;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.IOException;
import java.io.DataInputStream;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.ContentConnection;
import javax.microedition.io.Connector;
public class JpgMidlet extends MIDlet implements Runnable
{
private Display display;
private Thread thread;
private Frame frame;
public JpgMidlet() {
display = Display.getDisplay(this);
frame = new Frame();
}
protected void startApp() {
run();
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
private Image createImage(String name) throws IOException {
// Load from a ContentConnection
HttpConnection c = null;
DataInputStream is = null;
try {
c = (HttpConnection)Connector.open(name);
int status = c.getResponseCode();
if (status != 200) {
throw new IOException("HTTP Response Code = " + status);
}
int len = (int)c.getLength();
String type = c.getType();
if (len > 0) {
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);
return Image.createImage(data, 0, len);
} else {
throw new IOException("Content length is missing");
}
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
}
public void run() {
Image theimage = null;
String name = "http://127.0.0.1:11001/image.jpg";
try {
theimage = Image.createImage(name);
} catch (IOException err) {
}
frame.setImages(theimage);
display.setCurrent(frame);
}
}
class Frame extends Canvas {
private Image image;
Frame() {
image = Image.createImage(getWidth(), getHeight());
}
void setImages(Image theimage) {
Image image = theimage;
}
protected void paint(Graphics g) {
g.drawImage(image, 0, 0, Graphics.LEFT|Graphics.TOP);
}
}


Reply With Quote

