Sunday, February 17, 2013

Dirt simple video streaming

As mentioned in the last post, I found that even a very crude Java client could pull images from the Android phone's IP Webcam application at 320x200 fast enough for acceptable video. Good video streaming is a whole complex subject in itself, but I was surprised that this produced functional results. Here's standalone test code that's about as simple a streaming viewer as you are likely to find.

A simple launcher class:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*; 
import javax.imageio.*;
import java.awt.image.*;

import javax.swing.event.*;

import java.net.*;
import java.io.*;

public class launcher
{


public static void main(String[] args)
{
int count = 0;

MJpegViewer b = new MJpegViewer();

JFrame frame2 = new JFrame();
frame2.setSize(600,450);
frame2.setTitle("Video Feed");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(b);
 
//center frame on screen
//frame2.setLocationRelativeTo(null);
 
//set absolute screen position
frame2.setLocation(250, 0);
frame2.setVisible(true);

Image image = null;

try {
    URL url = new URL("http://192.168.2.104:8080/shot.jpg"); 
      
 

while (count < 10000)
 {  
    image = ImageIO.read(url);
  
 BufferedImage buffered = (BufferedImage) image;
 b.setBufferedImage(buffered);
 count = count + 1;
 }

} catch (IOException e) {}

}

}

The extended image frame:
import java.awt.*;
import javax.swing.JComponent;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.lang.Math;

public class MJpegViewer extends JComponent {

private static BufferedImage img = null;

public void setBufferedImage(BufferedImage newImg)
{
img = newImg;
repaint();
}



public void paintComponent(Graphics g) {
           
  //System.out.println("In paintComponent"); 
  g.drawImage(img, 0, 0, 600, 450, null);
      }


}

No comments:

Post a Comment