如何读取子图像光栅的数据数组
How to read data array of sub image raster
在this question ("get pixel array from image")中答案是获取栅格数据:
int[] pixels = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();
但是,当您将其用于 sub images 时,它将 return 基本图像的数据数组:
Returns a subimage defined by a specified rectangular region. The
returned BufferedImage shares the same data array as the original
image.
方法 getTileGridXOffset()
和 getTileGridYOffset()
return 偏移量尽管被描述为
Returns the x offset of the tile grid relative to the origin, For
example, the x coordinate of the location of tile (0, 0). This is
always zero.
但看起来无法访问获取数组中索引所需的栅格字段 scanlineStride
。
其他解决方案
使用 getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
会不会更快更容易?
具体例子
BufferedImage baseBufferedImage = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = baseBufferedImage.createGraphics();
graphics.setColor(Color.BLUE);
graphics.fillRect(512, 512, 100, 100);
graphics.dispose();
BufferedImage subBufferedImage = baseBufferedImage.getSubimage(512, 512, 100, 100);
int[] subBufferedImageData = ((DataBufferInt) subBufferedImage.getRaster().getDataBuffer()).getData();
// This is not 255 despite the pixel at 0,0 in subBufferedImage being blue
System.out.print(subBufferedImageData[0] & 0xFF);
如果你需要子图(作为BufferedImage
),你可以这样做:
BufferedImage subBufferedImage = baseBufferedImage.getSubimage(512, 512, 100, 100);
// NOTE: getData() creates a copy of the raster/databuffer in contrast to getRaster()
int[] subBufferedImageData = ((DataBufferInt) subBufferedImage.getData(new Rectangle(0, 0, 100, 100)).getDataBuffer()).getData();
System.out.print(subBufferedImageData[0] & 0xFF);
否则,您可以直接跳过子图像,直接创建光栅请求区域的副本,如下所示:
// Creates a copy of the sub region from the raster/databuffer
int[] subBufferedImageData = ((DataBufferInt) baseBufferedImage.getData(new Rectangle(512, 512, 100, 100)).getDataBuffer()).getData();
System.out.print(subBufferedImageData[0] & 0xFF);
两个示例都将打印 255
,并具有包含所有蓝色像素的数组。
您是否觉得使用 getRGB
更容易使用完全是主观的,取决于您。上面的代码可能会更快,或者在最坏的情况下也一样快(我将实际测试留给您)。
在this question ("get pixel array from image")中答案是获取栅格数据:
int[] pixels = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();
但是,当您将其用于 sub images 时,它将 return 基本图像的数据数组:
Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
方法 getTileGridXOffset()
和 getTileGridYOffset()
return 偏移量尽管被描述为
Returns the x offset of the tile grid relative to the origin, For example, the x coordinate of the location of tile (0, 0). This is always zero.
但看起来无法访问获取数组中索引所需的栅格字段 scanlineStride
。
其他解决方案
使用 getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
会不会更快更容易?
具体例子
BufferedImage baseBufferedImage = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = baseBufferedImage.createGraphics();
graphics.setColor(Color.BLUE);
graphics.fillRect(512, 512, 100, 100);
graphics.dispose();
BufferedImage subBufferedImage = baseBufferedImage.getSubimage(512, 512, 100, 100);
int[] subBufferedImageData = ((DataBufferInt) subBufferedImage.getRaster().getDataBuffer()).getData();
// This is not 255 despite the pixel at 0,0 in subBufferedImage being blue
System.out.print(subBufferedImageData[0] & 0xFF);
如果你需要子图(作为BufferedImage
),你可以这样做:
BufferedImage subBufferedImage = baseBufferedImage.getSubimage(512, 512, 100, 100);
// NOTE: getData() creates a copy of the raster/databuffer in contrast to getRaster()
int[] subBufferedImageData = ((DataBufferInt) subBufferedImage.getData(new Rectangle(0, 0, 100, 100)).getDataBuffer()).getData();
System.out.print(subBufferedImageData[0] & 0xFF);
否则,您可以直接跳过子图像,直接创建光栅请求区域的副本,如下所示:
// Creates a copy of the sub region from the raster/databuffer
int[] subBufferedImageData = ((DataBufferInt) baseBufferedImage.getData(new Rectangle(512, 512, 100, 100)).getDataBuffer()).getData();
System.out.print(subBufferedImageData[0] & 0xFF);
两个示例都将打印 255
,并具有包含所有蓝色像素的数组。
您是否觉得使用 getRGB
更容易使用完全是主观的,取决于您。上面的代码可能会更快,或者在最坏的情况下也一样快(我将实际测试留给您)。