gpt4 book ai didi

java - 使用 BufferedImage 的 DataBuffer 设置像素

转载 作者:行者123 更新时间:2023-11-29 05:52:40 25 4
gpt4 key购买 nike

我正在尝试使用 TYPE_3BYTE_BGR 类型的 BufferedImage 的底层 DataBufferByte 尽快设置像素值。

也许我没有理解,但是当我执行以下操作时......

byte[] imgBytes = ((DataBufferByte) img.getData().getDataBuffer()).getData();

...好像我得到的是 byte[] 的副本而不是引用。例如,如果我运行...

System.out.println(System.identityHashCode(imgBytes));
System.out.println(System.identityHashCode((DataBufferByte) img.getData().getDataBuffer()).getData());

...我得到两个明显不同的对象哈希值。如果我没记错的话,这表明我没有得到对底层 byte[] 的引用,而是一个副本。如果是这种情况,我应该如何直接编辑 DataBufferByte???

或者也许我只是设置了错误的像素...当我在 imgBytes 中设置像素时,它似乎对 BufferedImage 没有任何作用。获得 byte[] 后,我会像这样设置每个像素值:

imgBytes[intOffset] = byteBlue;
imgBytes[intOffset+1] = byteGreen;
imgBytes[intOffset+2] = byteRed;

对我来说,这一切似乎都很好。我可以通过这种方式很好地读取像素,所以看来我应该能够以相同的方式写入它们!

最佳答案

我遇到了同样的问题。您可能不使用 getData(),而是使用 getRaster(),它会为您提供一个可用于写入的数组。

关于java - 使用 BufferedImage 的 DataBuffer 设置像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328148/

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