Hi!
I'm currently developing a software that should connect to IP cameras
and display the video. The stream the camera sends is just a lot of
images with a small HTTP header. I've made a code that works right,
but it is quite slow and freezes sometimes. It seems that the problem
is when the program tries to load de image, I guess, with
Drawable.createFromStream().
Is there any ways I could use to get the code to run faster?
Here is my current code:
// MJPEG.java
package eldomus.mjpeg;
import java.util.*;
import java.io.*;
import java.net.*;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.widget.AdapterView.*;
import android.view.*;
import android.util.*;
import android.graphics.drawable.*;
public class MJPEG extends Activity {
static private final int DELAY = 200;
static public int x = 0;
private IPCam camera = null;
private ImageView img = null;
private Handler handler = new Handler() {
@Override public void handleMessage(Message msg) {
try {
Drawable x = camera.getImage();
img.setImageDrawable(x);
} catch(Exception e) {
printf("Error: " + e);
} finally {
printf("LoL!\n");
sleep();
};
};
public void sleep() {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), DELAY);
};
};
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
camera = new IPCam("161.28.137.220", 80, "/axis-cgi/mjpg/
video.cgi?camera=&resolution=320x240");
img = (ImageView)findViewById(R.id.image);
} catch(Exception e) {
printf("Error: " + e);
} finally {
printf("Start!");
};
handler.sendMessage(new Message());
};
private void printf(String str) {
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_SHORT).show();
};
};
// IPCam.java
package eldomus.mjpeg;
import java.util.*;
import java.io.*;
import java.net.*;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.widget.AdapterView.*;
import android.view.*;
import android.util.*;
import android.graphics.drawable.*;
class ContinuedStream extends InputStream {
private InputStream base = null;
private Scanner scanner = null;
ContinuedStream(InputStream x) {
base = x;
scanner = new Scanner(this);
};
public void close() {
// Do nothing!
};
public int read(byte[] b, int offset, int len) throws IOException {
return base.read(b, offset, len);
};
public int read(byte[] b) throws IOException {
return base.read(b);
};
public int read() throws IOException {
return base.read();
};
public String readLine() throws IOException {
String result = "";
char x = (char)read();
main: while(true) {
while(x == '\r') {
x = (char)read();
if(x == '\n') {
break main;
};
result += x;
};
result += x;
x = (char)read();
};
return result;
};
public String readHeader() throws IOException {
String result = "";
while(true) {
String tmp = readLine();
if(tmp.length() == 0) {
break;
};
result += tmp;
result += "\r\n";
};
return result;
};
};
public class IPCam {
private Socket socket = null;
private PrintStream out = null;
private ContinuedStream in = null;
public IPCam(String host, int port, String file) throws Exception {
socket = new Socket(host, port);
out = new PrintStream(socket.getOutputStream());
in = new ContinuedStream(socket.getInputStream());
write("GET " + file + " HTTP/1.0\r\nKeep-Alive: 300\r\nConnection:
keep-alive\r\n");
in.readHeader();
};
public Drawable getImage() throws Exception {
String str = in.readHeader();
MJPEG.x = str.length();
Drawable img = Drawable.createFromStream(in, "src");
return img;
};
private void write(String s) {
out.print(s + "\r\n");
out.flush();
};
};
Thank you.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en