Results 1 to 4 of 4
  1. #1
    nesrine18 is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Exclamation Capture Video from ip camera

    hi,
    i'm trying to capture video from my IP camera..
    i did this,but i don't know what's the problem..there's no errors but i can't see the video in my Jframe
    can you help me?

    this is my code

    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.;


    /*

    @author David E. Mireles, Ph.D.
    /
    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_CLO SE);
    AxisCamera axPanel = new AxisCamera(jframe);
    new Thread(axPanel).start();
    jframe.getContentPane().add(axPanel);
    jframe.pack();
    jframe.show();
    }


    }

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    My first guess is that paint is never getting called. The 'right' way to subclass swing components is to subclass JComponent and override the 'paintComponent' method. This ensures that you don't interfere with the repaint queue but play nicely with it instead. Then add your JComponent extension to a container that has a valid layout manager. You can setlayout(new BorderLayout()) on the parent container just to be sure.

    ...if it is...

    It would help if you use the code tags. If I were debugging this, I'd start in readJPEG and see whether or not imgage is null (I'm betting it is). If so, then I'd work my way back to figure out why. Alternately, you can start at the beginning and verify that your connections are valid and step your way forward to see what assumptions break down.

  3. #3
    Join Date
    Nov 2011
    Location
    Lima, Perú
    Posts
    2
    Rep Power
    0

    Default Re: Capture Video from ip camera

    This post: Rolando Palermo Rodríguez Cruz: Conectándose a una cámara IP con Java shows how to connect an IP cam with a Swing Java App.
    Last edited by DarrylBurke; 03-12-2012 at 09:09 PM. Reason: Broke blog link

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Capture Video from ip camera

    rolandopalermo, stop posting to old long dead threads. That doesn't help anyone.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Video Frame Capture
    By brainstech in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-17-2010, 05:08 PM
  2. camera pic
    By Aveekdtt in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-10-2010, 01:52 AM
  3. please help me in accessing integrated camera of PDA ???????
    By sri_reddy523 in forum CLDC and MIDP
    Replies: 1
    Last Post: 03-19-2009, 08:05 AM
  4. Loading a camera
    By mrvigneshmca in forum Java Applets
    Replies: 0
    Last Post: 03-18-2009, 11:46 AM
  5. Replies: 0
    Last Post: 11-20-2008, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •