2016-02-13 182 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