Threads to make video out of images
Posted
by
masood
on Stack Overflow
See other posts from Stack Overflow
or by masood
Published on 2012-11-03T11:24:10Z
Indexed on
2012/11/04
11:01 UTC
Read the original article
Hit count: 238
updates: I think/ suspect the imageIO is not thread safe. shared by all threads. the read() call might use resources that are also shared. Thus it will give the performance of a single thread no matter how many threads used. ? if its correct . what is the solution (in practical code)
Single request and response model at one time do not utilizes full network/internet bandwidth, thus resulting in low performance. (benchmark is of half speed utilization or even lower)
This is to make a video out of an IP cam that gives a new image on each request.
http://149.5.43.10:8001/snapshot.jpg
It makes a delay of 3 - 8 seconds no matter what I do.
Changed thread no. and thread time intervals,
debugged the code by System.out.println
statements to see if threads work. All seems normal. Any help?
Please show some practical code. You may modify mine.
This code works (javascript)
with much smoother frame rate and max bandwidth usage. but the later code (java) dont
. same 3 to 8 seconds gap.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(function(){
var img="/*url*/";
var interval=50;
var pointer=0;
function showImg(image,idx)
{
if(idx<=pointer) return;
document.body.replaceChild(image,document.getElementsByTagName("img")[0]);
pointer=idx;
preload();
}
function preload()
{
var cache=null,idx=0;;
for(var i=0;i<5;i++)
{
idx=Date.now()+interval*(i+1);
cache=new Image();
cache.onload=(function(ele,idx){return function(){showImg(ele,idx);};})(cache,idx);
cache.src=img+"?"+idx;
}
}
window.onload=function(){
document.getElementsByTagName("img")[0].onload=preload;
document.getElementsByTagName("img")[0].src="/*initial url*/";
};
})();
</script>
</head>
<body>
<img />
</body>
</html>
and of java (with problem) :
package camba;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.security.Timestamp;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.imageio.ImageIO;
public class Camba extends Applet implements ActionListener{
Image img;
TextField textField;
Label label;
Button start,stop;
boolean terminate = false;
long viewTime;
public void init(){
label = new Label("please enter camera URL ");
add(label);
textField = new TextField(30);
add(textField);
start = new Button("Start");
add(start);
start.addActionListener(this);
stop = new Button("Stop");
add(stop);
stop.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Button source = (Button)e.getSource();
if(source.getLabel() == "Start"){
for (int i = 0; i < 7; i++) {
myThread(50*i);
}
System.out.println("start...");
}
if(source.getLabel() == "Stop"){
terminate = true;
System.out.println("stop...");
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g){
try{
viewTime = System.currentTimeMillis();
g.drawImage(img, 100, 100, this);
} catch(Exception e) {
e.printStackTrace();
}
}
public void myThread(final int sleepTime){
new Thread(new Runnable() {
public void run() {
while(!terminate){
try {
TimeUnit.MILLISECONDS.sleep(sleepTime);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
long requestTime= 0;
Image tempImage = null;
try {
URL pic = null;
requestTime= System.currentTimeMillis();
pic = new URL(getDocumentBase(), textField.getText());
tempImage = ImageIO.read(pic);
} catch(Exception e) {
e.printStackTrace();
}
if(requestTime >= /*last view time*/viewTime){
img = tempImage;
Camba.this.repaint();
}
}
}}).start();
System.out.println("thread started...");
}
}
© Stack Overflow or respective owner