2009-10-16 59 views

回答

65
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes)); 
+6

這不回答這個問題,問題是用於寫入圖像文件。這個答案適用於讀取圖像文件。什麼與所有的最高票? – Sixtoo 2015-12-25 15:50:33

+5

標題說,但問題是他們有一個字節數組,需要一個圖像,這就是它的作用。 – 2016-01-29 18:49:03

1

根據Java文檔,它看起來像你需要使用the MemoryImageSource Class把你的字節數組到內存中的對象,然後使用Component.createImage(ImageProducer的)下一個(通過在MemoryImageSource ,它實現ImageProducer)。

20

如果您知道圖像的類型並且只想生成一個文件,則不需要獲取BufferedImage實例。只需將字節寫入具有正確擴展名的文件即可。

OutputStream out = null; 

try { 
    out = new BufferedOutputStream(new FileOutputStream(path)); 
    out.write(bytes); 
} finally { 
    if (out != null) out.close(); 
} 
2
From Database. 
Blob blob = resultSet.getBlob("pictureBlob");    
byte [] data = blob.getBytes(1, (int) blob.length()); 
BufferedImage img = null; 
try { 
img = ImageIO.read(new ByteArrayInputStream(data)); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
drawPicture(img); // void drawPicture(Image img);