package photo; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.IOException; import java.io.*; import javax.microedition.*; import javax.microedition.io.HttpConnection; import javax.microedition.io.Connector; import java.*; import java.io.InputStream; import java.io.OutputStream; public class PhotoAlbum extends MIDlet implements CommandListener, ItemStateListener { private Display display; public String imageName; MJPEGParserA parser; private PhotoFrame canvas; HttpConnection c = null; InputStream is = null; OutputStream os = null; class MJPEGParserA { private final byte[] JPEG_START = new byte[] { (byte) 0xFF, (byte) 0xD8 }; private final int INITIAL_BUFFER_SIZE = 4096; InputStream in; byte[] boundary; byte[] segment; byte[] buf; int cur, len; boolean canceled = false; 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) { append(b); if (checkBoundary()) { processSegment(); cur = 0; } } } protected void processSegment() { Image source; 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); source = Image.createImage(segment, 0, segLength); canvas.drawImage(source); display.setCurrent(canvas); } } 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; } } void postViaHttpConnection(String url) throws IOException { String boundary = "--myboundary"; try { c = (HttpConnection)Connector.open(url); is = c.openInputStream(); parser = new MJPEGParserA(is, boundary); parser.parse(); } catch (ClassCastException e) { throw new IllegalArgumentException("Not an HTTP URL"); } finally { if (is != null) is.close(); if (os != null) os.close(); if (c != null) c.close(); } } public PhotoAlbum() { display = Display.getDisplay(this); canvas = new PhotoFrame(this); canvas.setCommandListener(this); } protected void startApp() { try { postViaHttpConnection("http://217.128.151.33/axis-cgi/mjpg/video.cgi?camera=1@fps=25@resolution=160x120"); //217.128.151.33 } catch(IOException e) { } } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { } public void itemStateChanged(Item item) { } }