2016-02-13 98 views
2

是否可以在java中編輯圖像? 我的意思是在特定點上以某種RGB顏色繪製像素並保存圖像。Java - 編輯圖像

我正在做一個遊戲,其中的對象是由圖像加載,爲了保存地圖的當前狀態,我需要編輯一些像素並稍後加載。

任何幫助表示讚賞! :)

+3

http://stackoverflow.com/questions/5702397/edit-pixel-values –

+0

我會研究它,謝謝! – Artur

回答

1

是的。如果您創建一個BufferedImage實例,它是一個存儲圖像數據的對象,您將能夠獲取像素並進行更改。這是如何:

public static void main(String[] args) throws Exception { 

    BufferedImage originalImage = ImageIO.read(inputFile); 
    BufferedImage newImage = orgiginalImage; 
    int[] pixels = ((DataBufferInt)newImage.getRaster().getDataBuffer()).getData(); 

    for(int i = 0; i < pixels.length; i++){ 
     // Code for changing pixel data; 
     pixels[i] = 0xFFFFFFFF // White 
     // Syntax for setting pixel color: 0x(HEX COLOR CODE) 
     // There is no need to set these pixels to the image; they are allerady linked 
     // For instance, if you create a Canvas object in a JFrame, 
     // and used graphics.drawImage(newImage, 0, 0, 
     // newImage.getWidth(), newImage.getHeight(), null), it will be up to date 
     // Another example is, if you saved newImage to a file, it willallready have 
     // the white pixels drawn in. 
    } 

} 
+0

感謝您的幫助! 我已經做了一點不同,一旦我開始一個新遊戲,我創建一個新的圖像,並使用BufferedImage => image.setRGB(x,y,rgb)編輯它,當我刪除/ – Artur