MJPG-Streamer客户端的关键是读取JPG文件。
JPG文件 头标识 0xff 0xd8 尾标识 0xff 0xd9
把中间取出来放到JLabel中就是视频。
@Override
public void run() {
byte[] ca = new byte[128*1024];
byte[] cab = new byte[328 * 1024];
int i=0;
int status=0;
int cabindex=0;
int js=0,jss=0,jj=0;
try {
// ur = new URL("http://192.168.3.16:8082/?action=stream");
ur= new URL("http://192.168.0.102:8080/?action=stream");
URLConnection uc = ur.openConnection();
uc.connect();
InputStream is=uc.getInputStream();
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(new File("D:\\NetBeansProjects\\JavaApplication1\\src\\tcp\\N.jp")));
while((jj=is.read(ca))!=-1){
// ll.setText(String.valueOf(is.()));
for(i=0;i<jj;i++){
if(ca[i]==(byte)0xff){
if(ca[i+1]==(byte)0xd8){
status=1;
jss++;
ll.setText(String.valueOf(jss));
}
}
if(status==1){
cab[cabindex]=ca[i];
cabindex++;
if(i!=0){
if(ca[i-1]==(byte)0xff){
if(ca[i]==(byte)0xd9){
ll2.setText(String.valueOf(js));
status=0;
cabindex=0;
js++;
ll.setIcon(new javax.swing.ImageIcon(cab));
}
}
}
}
}
}
} catch (MalformedURLException ex) {
Logger.getLogger(vedio.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(vedio.class.getName()).log(Level.SEVERE, null, ex);
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}