2012-06-25 132 views
2

我想在图像捕捉来自IP摄像机的视频到我的应用程序,它让例外从IP摄像头捕获视频

com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x0d 0x0a 
    at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method) 
    at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(Unknown Source) 
    at test.AxisCamera1.readJPG(AxisCamera1.java:130) 
    at test.AxisCamera1.readMJPGStream(AxisCamera1.java:121) 
    at test.AxisCamera1.readStream(AxisCamera1.java:100) 
    at test.AxisCamera1.run(AxisCamera1.java:171) 
    at java.lang.Thread.run(Unknown Source) 

其给予例外= decoder.decodeAsBufferedImage();

这里是代码我试图

private static final long serialVersionUID = 1L; 
    public boolean useMJPGStream = true; 
    public String jpgURL = "http://ip here/video.cgi/jpg/image.cgi?resolution=640×480"; 
    public String mjpgURL = "http://ip here /video.cgi/mjpg/video.cgi?resolution=640×480"; 
    DataInputStream dis; 
    private BufferedImage 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 AxisCamera1(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() { 
     System.out.println("in Run..................."); 
     connect(); 
     readStream(); 
    } 

    @SuppressWarnings("deprecation") 
    public static void main(String[] args) { 


     JFrame jframe = new JFrame(); 
     jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     AxisCamera1 axPanel = new AxisCamera1(jframe); 
     new Thread(axPanel).start(); 
     jframe.getContentPane().add(axPanel); 
     jframe.pack(); 
     jframe.show(); 
    } 

} 

任何建议什么,我做错了什么?

回答

2

jpeg魔法代码是“0xff 0xd8”而不是“0x0d 0x0a”(请参阅​​[1]),这就是为什么代码失败的原因。也许这条在readMJPGStream()行代码的部分是饿了:)

[1] http://en.wikipedia.org/wiki/JPEG

+0

在此代码剪断尝试读取MJPG流,我敢肯定,你必须做的更好比只是删除两条第一条线。也许这可以帮助:http://code.google.com/p/ipcapture/source/browse/IPCapture.java?r=0df4452208266f77fdc09b427682eaee09054fcb – Arcadien

+0

感谢您的帮助..问题是与阅读mjpg的线...终于我得到它的工作.. :) – Ruby