gpt4 book ai didi

Java - 编辑图像

转载 作者:行者123 更新时间:2023-11-30 10:49:31 24 4
gpt4 key购买 nike

是否可以在 java 中编辑图像?我的意思是在某个地方以某种 RGB 颜色绘制一个像素并保存图像。

我正在开发一个游戏,其中对象由图像加载,为了保存 map 的当前状态,我需要编辑一些像素并稍后加载它。

感谢任何帮助! :)

最佳答案

是的。如果您创建 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.
}

}

关于Java - 编辑图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com