2011-11-14 86 views
1

我要顯示的圖像,因爲它正在下載,我有URL,我想獲得這樣的圖像部分:逐行圖像擺動

InputStream openStream = url.openStream(); 

     DataInputStream dis = new DataInputStream(new BufferedInputStream(openStream)); 
     ByteArrayOutputStream os = new ByteArrayOutputStream(); 

     while ((s = dis.read(b)) != -1) { 

      os.write(b , 0, s); 

      support.firePropertyChange("stream", null, os); 

     } 

這樣,任何一個聽衆獲得流和創建圖像時,這樣說:

if("stream".equals(evt.getPropertyName())){ 
     try { 
      ByteArrayOutputStream stream = (ByteArrayOutputStream) evt.getNewValue(); 
      byte[] byteArray = stream.toByteArray(); 
      stream.flush(); 
      Image createImage = Toolkit.getDefaultToolkit().createImage(byteArray); 

      this.getContentPane().add(new JLabel(new ImageIcon(createImage))); 

     } catch (IOException ex) { 
      Logger.getLogger(ImageTest.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

但是,我得到一個「JPEG文件sun.awt.image.ImageFormatException過早結束:JPEG數據流內沒有圖像」的錯誤,該圖像是一個JPG圖像格式,是否有任何已知的類似的庫或方法?

回答