gpt4 book ai didi

java - 如何操作BufferedImage

转载 作者:行者123 更新时间:2023-12-01 18:47:29 25 4
gpt4 key购买 nike

Processing有一个名为 PImage 的类从中你可以获得 int 的数组包含所有像素的值。然后,您操作该数组并调用 updatePixels()瞧,您已经对图像应用了效果。

我想知道是否可以在 BufferedImage 中完成同样的操作有一些适当的机制。我发现BufferedImage确实有一个方法来获取像素 int[] :

public int[] getRGB(int startX,
int startY,
int w,
int h,
int[] rgbArray,
int offset,
int scansize)
Returns an array of integer pixels in the default RGB color model (`TYPE_INT_ARGB`) and default sRGB color space, from a portion of the image data.
Color conversion takes place if the default model does not match the image `ColorModel`.
There are only 8-bits of precision for each color component in the returned data when using this method.

如何修改这些像素并在 BufferedImage 中显示适当的更改?

我想我需要获得 WritableRaster对于图像并使用

public void setPixels(int x,
int y,
int w,
int h,
int[] iArray)

但我还是不确定。

最佳答案

要创建WritableRaster,您必须首先选择ColorModel。我认为 RGB 默认值应该适合您的需求。

ColorModel colorModel = ColorModel.getRGBdefault();
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height);

然后,您可以用像素填充它并创建一个新的 BufferedImage

raster.setPixels(0, 0, width, height, pixels);      
BufferedImage image = new BufferedImage(colorModel, raster, true, null);

提醒一下,这里有一种从 BufferedImage 中提取像素的方法:

Raster raster = bufferedImage.getRaster();
int[] pixels = raster.getPixels(0, 0, raster.getWidth(), raster.getHeight(), (int[]) null);

关于java - 如何操作BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161335/

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