2011-12-12 44 views
0

你能做這樣的事情來獲取緩衝圖像作爲文件位置嗎?來自BufferedImage的新FileDataSource?

DataSource source = new FileDataSource(ImageIO.write(image, "png", file)); 

我試圖發送一個緩衝的形象,使用Java郵件API一個附件,而不必首先保存文件?

TIA

回答

1

我工作圍繞這個做節目的一個相當跛腳一點,但它的工作原理:

File file = new File("temp.png"); 
ImageIO.write(originalImage, "png", file); 
DataSource source = new FileDataSource(file); 

然後,當它完成我只是:

+0

不錯的工作環繞xD不理想的解決方案,但工作速度很快 – DarkCygnus

0

沒有不作爲編譯返回ImageIO.write一個booleanFileDataSource預計在其構造

3

文件/文件名構建自己的DataSource類,並從一個ByteArrayInputStream包裝把你的輸入流圖像字節。

喜歡的東西:

public class ImageDataSource implements DataSource { 

private Image image = null; 
private String imageName = ""; 

public ImageDataSource(Image image, String imageName) { 
    this.image = image; 
    this.imageName = imageName; 
}//cons 

public InputStream getInputStream() { 
    return new ByteArrayInputStream(image.getBytes()); 
}//met 

public OutputStream getOutputStream() { 
    throw new IOException(); 
}//met 

public String getName() { 
    return imageName; 
}//met 

public String getMimeType() { 
    return "image/png"; 
}//met 

private byte [] getImgBytes(Image image) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try { 
     ImageIO.write(getBufferedImage(image), "png", baos); 
    } catch (IOException ex) { 
     //handle it here.... not implemented yet... 
    } 
    return baos.toByteArray(); 
}//met 

}//class 

或多或少,我得到了不IDE :)

+0

+1爲建議的解決方案 – GETah

+0

謝謝我會放棄,期待更多的問題,看起來很複雜! –

+0

由此引發的很多錯誤,我無法全部解決。是不是有一種更簡單的方法來獲得ByteArrayInputStream與整個班級? –