Hej jag har gjort en app där jag vill ha ett fönster där jag ser video från en axis nätverkskamera. Det ända sätt jag har kommit på att lösa detta är igenom att hämta en snapshot från kamera och sedan uppdatera en ImageView från en egen thread.
Det funkar bra men det laggar en del. Är det någon som vet om jag kan göra det på ett bättre sätt? Skulle jag kunna ta en video stream från kameran direkt?
Kod:
public class Video implements Runnable {
// Constructor
public Handler videoHandler;
Video(Handler aHandler) {
videoHandler = aHandler;
}
private String IP = "", address = "";
private boolean run;
// Method to start the thread
public void run() {
try {
run = true;
// Run until stopped
while (run) {
// URL to camera
IP = AndonGUIActivity.IPaddress;
address = "http://" + IP + "/axis-cgi/jpg/image.cgi";
Log.d("WebService", address);
// Open connection
final URL aURL = new URL(address);
final URLConnection conn = aURL.openConnection();
conn.connect();
// Open buffer to read image stream
final BufferedInputStream bis = new BufferedInputStream(conn
.getInputStream());
// Make image of the input stream
final Bitmap bm = BitmapFactory.decodeStream(bis);
// Close input stream
bis.close();
// Update an image view with the downloaded image
videoHandler.post(new Runnable() {
public void run() {
AndonGUIActivity.cameraVideo.setImageBitmap(bm);
}
});
}
} catch (IOException e) {
displayStatus(" ERROR: " + e);
}
}
// Method to stop the thread
public void stop() {
run = false;
videoHandler = null;
Log.d("WebService", "Stopped");
}
// Method to update a status label in the UI thread
public void displayStatus(final String status) {
AndonGUIActivity.status.post(new Runnable() {
public void run() {
AndonGUIActivity.status.append(status);
}
});
}
}