Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: retrieve the video stream from an Axis camera

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default retrieve the video stream from an Axis camera

    hi everyone,
    i'm a new member of this forum..i'm trying to retrieve the video stream from my Axis camera..i don't know what's exactly the problem:when i run the project there's no errors but i get no video on my jFrame..
    this is my code,can you please help me to know where the problem is?
    thanks..


    import java.net*;
    import com.sun.image.codec.jpeg*;
    import java.io*;
    import java.awt*;
    import java.awt.event*;
    import java.awt.image*;
    import javax.swing*;
     
     
    public class AxisCamera extends JPanel implements Runnable {
    public boolean useMJPGStream = true;
    public String jpgURL="http://your-ip-here/axis-cgi/jpg/image.cgi?resolution=352x240";
    public String mjpgURL="http://your-ip-here/axis-cgi/mjpg/video.cgi?resolution=352x240";
    DataInputStream dis;
    private Image image=null;
    public Dimension imageSize = null;
    public boolean connected = false;
    private boolean initCompleted = false;
    HttpURLConnection huc=null;
    Component parent;
     
    /** Creates a new instance of AxisCamera */
    public AxisCamera (Component parent_) {
    parent = parent_;
    }
     
    public void connect(){
    try{
    URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
    huc = (HttpURLConnection) u.openConnection();
     
     
     
     
     
     
     
    //System.out.println(huc.getContentType());
    InputStream is = huc.getInputStream();
    connected = true;
    BufferedInputStream bis = new BufferedInputStream(is);
    dis= new DataInputStream(bis);
    if (!initCompleted) initDisplay();
    }catch(IOException e){ //incase no connection exists wait and try again, instead of printing the error
    try{
    huc.disconnect();
    Thread.sleep(60);
    }catch(InterruptedException ie){huc.disconnect();connect();}
    connect();
    }catch(Exception e){;}
    }
     
    public void initDisplay(){ //setup the display
    if (useMJPGStream)readMJPGStream();
    else {readJPG();disconnect();}
    imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
    setPreferredSize(imageSize);
    parent.setSize(imageSize);
    parent.validate();
    initCompleted = true;
    }
     
    public void disconnect(){
    try{
    if(connected){
    dis.close();
    connected = false;
    }
    }catch(Exception e){;}
    }
     
    public void paint(Graphics g) { //used to set the image on the panel
    if (image != null)
    g.drawImage(image, 0, 0, this);
    }
     
    public void readStream(){ //the basic method to continuously read the stream
    try{
    if (useMJPGStream){
    while(true){
    readMJPGStream();
    parent.repaint();
    }
    }
    else{
    while(true){
    connect();
    readJPG();
    parent.repaint();
    disconnect();
     
    }
    }
     
    }catch(Exception e){;}
    }
     
     
    public void readMJPGStream(){ //preprocess the mjpg stream to remove the mjpg encapsulation
    readLine(3,dis); //discard the first 3 lines
    readJPG();
    readLine(2,dis); //discard the last two lines
    }
     
    public void readJPG(){ //read the embedded jpeg image
    try{
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
    image = decoder.decodeAsBufferedImage();
    }catch(Exception e){e.printStackTrace();disconnect();}
    }
     
    public void readLine(int n, DataInputStream dis){ //used to strip out the header lines
    for (int i=0; i<n;i++){
    readLine(dis);
    }
    }
    public void readLine(DataInputStream dis){
    try{
    boolean end = false;
    String lineEnd = "\n"; //assumes that the end of the line is marked with this
    byte[] lineEndBytes = lineEnd.getBytes();
    byte[] byteBuf = new byte[lineEndBytes.length];
     
    while(!end){
    dis.read(byteBuf,0,lineEndBytes.length);
    String t = new String(byteBuf);
    //System.out.print(t); //uncomment if you want to see what the lines actually look like
    if(t.equals(lineEnd)) end=true;
    }
    }catch(Exception e){e.printStackTrace();}
     
     
    }
    public void run() {
    connect();
    readStream();
    }
     
     
    public static void main(String[] args) {
    JFrame jframe = new JFrame();
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AxisCamera axPanel = new AxisCamera(jframe);
    new Thread(axPanel).start();
    jframe.getContentPane().add(axPanel);
    jframe.pack();
    jframe.show();
    }
     
     
    }
    Last edited by helloworld922; April 3rd, 2010 at 10:28 AM.


Similar Threads

  1. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM
  2. Client input Stream
    By gisler in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 19th, 2009, 09:30 PM
  3. jmf , video with hopes of AR
    By wolfgar in forum Java SE APIs
    Replies: 0
    Last Post: December 4th, 2009, 04:54 AM
  4. Video Class
    By Swankee in forum Java SE APIs
    Replies: 2
    Last Post: October 8th, 2009, 11:19 AM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM