通过阵列操作BufferedImage中的像素 [英] Manipulating the pixels within a BufferedImage through an Array

查看:189
本文介绍了通过阵列操作BufferedImage中的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从头开始关于Java游戏开发的系列文章。我理解大多数java和oop概念,但在处理图形和硬件加速时几乎没有经验。

I'm currently following a series on Java game development from scratch. I understand most java and oop concepts but have very little experience when dealing with graphics and hardware acceleration.

我质疑的代码行包括:

The lines of code that I am questioning are:

 private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

BufferedImageimage变量始终是通过渲染方法绘制到屏幕上的变量。通常是这样的:

The BufferedImage "image" variable is always the variable being drawn to the screen through a render method. Usually something like:

 public void render() {
    BufferStrategy bs = this.getBufferStrategy;
    if(bs == null) { this.createBufferStrategy(3); return; }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();
 }

我知道像素数组包含BufferedImage中的每个像素,但是,它好像每次该数组填充值都会直接影响图像变量的内容。从来没有一种方法用于将像素数组值复制到图像中。

I understand the array of pixels contains every pixel within the BufferedImage, however, it seems as though everytime that array is filled with values it directly effects the contents of the "image" variable. There is never a method used to copy the pixels array values into the image.

这些变量是否以这种方式实际链接?是否使用:

Are these varibales actually linked in such a way? Does the use of:

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

在上面的代码中创建图像和数组之间的自动链接?也许我正在进行cray,只是错过了一些东西,但我已经多次查看了代码,并且在初始创建之后不再一次是图像varibale被操纵。 (除了当然是渲染到屏幕上)。总是数组像素被填充不同的值,导致渲染图像发生变化。

create an automated link between the image and the array being created in the code above? Maybe I am going cray and just missed something but I have reviewed the code several times and not once is the "image" varibale manipulated after it's initial creation. (besides being rendered to the screen of course.) It's always the array "pixels" just being filled with different values that causes the change in the rendered image.

一些见解在这将是美好的。

Some insight on this would be wonderful. Thank you in advance!

推荐答案

您为什么不打电话给我?

Why don't you call

image.getData()

而不是

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

image.getData()返回Raster的副本。 getRaster()返回一个可修改像素的WriteableRaster。我在猜测,但getRaster()可能会返回图像栅格的子元素,因此如果修改该数组是可写的。试试image.getData()来查看它是否工作。如果没有,请回到这里,我会仔细看看。

image.getData() returns a copy of the Raster. getRaster() returns a WriteableRaster with the ability to modify pixels. I'm guessing but getRaster() probably returns a child of the image Raster and therefore is writeable if you modify the array. Try image.getData() to see if it works. If not, post back here and I'll take a closer look.

我进一步研究了这一点。 JDK附带的源代码显示 image.getRaster()。getDataBuffer()。getData()返回源数据数组。 image.getData()确实返回一个副本。如果图像被修改,getData()中的数据将不会被修改。

I looked into this further. The source code that comes with the JDK shows that image.getRaster().getDataBuffer().getData() returns the source data array. image.getData() indeed returns a copy. If the image is modified, the data in getData() will not be modified.

您可以在返回的Raster上调用getPixels:

You can call getPixels on the returned Raster:

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




返回一个int数组,其中包含矩形的所有样本像素,
每个数组元素一个样本。如果坐标不在边界内,ArrayIndexOutOfBoundsException可能是
抛出。但是,显式范围
检查不能保证。

Returns an int array containing all samples for a rectangle of pixels, one sample per array element. An ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds. However, explicit bounds checking is not guaranteed.

这篇关于通过阵列操作BufferedImage中的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆