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
|
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");
}
catch(IOException e)
{
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
}
public void itemStateChanged(Item item) {
}
} |
Partager