簡體   English   中英

如何將圖像轉換為BufferedImage

[英]How to convert Image into BufferedImage

現在我有一個圖像,我想將其轉換為BufferedImage ...

這是我的代碼-

private BufferedImage toBufferedImage(Image img, int width, int height){
    // Create a buffered image with transparency
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics bGr = bimage.getGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    // Return the buffered image
    return bimage;
}

但這是行不通的。 有人可以解釋什么地方嗎?

BufferedImage擴展了Image,因此您可以投射它:

BufferedImage buffered = (BufferedImage) image;

但是您必須在運行時檢查圖像對象的類型,因為它可以包含其他類型的圖像。

UPDATE

要將圖像轉換為BufferedImage,應檢查此類並嘗試:

http://www.dzone.com/snippets/converting-images

嘗試包含JavaFX的新JRE。

您可以使用SwingFXUtils。 有一種方法可以滿足您的要求:

BufferedImage image = SwingFXUtils.fromFXImage(fxImage, null);

要么

BufferedImage image = ImageIO.read(new File(filename));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM