简体   繁体   English

使用BufferedImage的DataBuffer来设置像素

[英]Using DataBuffer of BufferedImage to set pixels

I am trying to use the underlying DataBufferByte of a BufferedImage of type TYPE_3BYTE_BGR to set pixel values as quick as possible. 我正在尝试使用TYPE_3BYTE_BGR类型的BufferedImage的基础DataBufferByte来尽可能快地设置像素值。

Perhaps I am not understanding, but when I do the following... 也许我不理解,但是当我做以下事情时......

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

... it seems as though I am getting a copy of the byte[] and not a reference. ...好像我得到了byte []的副本,而不是引用。 For example, if I run... 例如,如果我跑......

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

... I get two clearly different object hashes. ...我得到两个明显不同的对象哈希。 If I'm not mistaken, this indicates that I am not getting a reference to the underlying byte[] but rather a copy. 如果我没有弄错,这表明我没有得到对底层字节[]的引用,而是一个副本。 If this is the case, how am I supposed to edit the DataBufferByte directly??? 如果是这种情况,我该如何直接编辑DataBufferByte ???

Or perhaps I am just setting the pixels wrong... When I set pixels in the imgBytes it doesn't seem to do anything to the BufferedImage. 或许我只是设置像素错误...当我在imgBytes中设置像素时,它似乎对BufferedImage没有任何作用。 Once I get the byte[], I set each pixel value like so: 一旦我得到byte [],我就像这样设置每个像素值:

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

To me, this all seems fine. 对我来说,这一切似乎都很好。 I can read pixels just fine this way so it seems I should be able to write them the same way! 我可以通过这种方式读取像素,所以看起来我应该能够以同样的方式编写它们!

I had the same problem. 我有同样的问题。 You may not use getData() but use getRaster() which gives you an array you can use to write to. 你可能不会使用getData(),而是使用getRaster(),它会为你提供一个可以用来写入的数组。

I once played around with pixel manipulations for Images in Java. 我曾经玩过Java中的图像像素处理。 Instead of directly answering your question I will offer an alternative solution to your problem. 我会提供替代解决方案,而不是直接回答您的问题。 You can do the following to create an array of pixels to manipulate: 您可以执行以下操作来创建要操作的像素数组:

final int width = 800;
final int height = 600;
final int[] pixels = new int[width * height]; // 0xAARRGGBB
MemoryImageSource source = new MemoryImageSource(width, height, pixels, 0, width);
source.setAnimated(true);
source.setFullBufferUpdates(true);
Image image = Toolkit.getDefaultToolkit().createImage(source);
image.setAccelerationPriority(1f);

Then to draw the image, you can simply call the drawImage method from the Graphics class. 然后要绘制图像,只需从Graphics类中调用drawImage方法即可。

There are a few other ways to achieve what you are looking for, but this method was the simplest to me. 还有其他几种方法可以实现您的目标,但这种方法对我来说最简单。

Here is how it's implemented in JDK7. 以下是它在JDK7中的实现方式。 You may have an error somewhere else if the stuff doesn't work for you. 如果这些东西不适合您,您可能在其他地方出现错误。

public byte[] getData() {
    theTrackable.setUntrackable();
    return data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM