BufferedImage :提取具有相同数据的子图像
BufferedImage : extract subimage with same data
我想提取 BufferedImage 的矩形。
Javadoc 提出 getSubImage(x,y,w,h) 和 getData(rectangle)。
getData 很酷,但我不想要光栅。我希望子图像作为 BufferedImage 对象,但我还需要它的数据数组的修改版本,但 javadoc 说
public BufferedImage getSubimage(int x,int y,int w,int h) : Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
问:如何提取具有收缩数据数组的子图像?
很久以前我遇到过同样的问题,我不想要共享栅格。我找到的唯一解决方案是创建一个表示子图像的 BufferedImage,然后将像素复制到子图像中。
为了获得真正快速的东西,我直接访问 DataBuffer 并使用 System.arraycopy()
制作数组副本(逐行)
给定一个 BufferedImage
图像,这里有 3 种创建 "deep" 复制子图像的方法:
// Create an image
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
// Fill with static
new Random().nextBytes(((DataBufferByte) image.getRaster().getDataBuffer()).getData());
围绕您从 getData(rect)
获得的 Raster
的已经很深的副本创建图像。这涉及转换为 WritableRaster
,因此它可能会与某些 Java 实现或将来中断。应该很快,因为你只复制一次数据:
// Get sub-raster, cast to writable and translate it to 0,0
WritableRaster data = ((WritableRaster) image.getData(new Rectangle(25, 25, 50, 50))).createWritableTranslatedChild(0, 0);
// Create new image with data
BufferedImage subOne = new BufferedImage(image.getColorModel(), data, image.isAlphaPremultiplied(), null);
另一个选项,创建子图像 "normal way",然后将光栅复制到新图像中。涉及创建一个子光栅,仍然只复制一次(并且没有投射):
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create empty compatible image
BufferedImage subTwo = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(50, 50), image.isAlphaPremultiplied(), null);
// Copy data into the new, empty image
subimage.copyData(subTwo.getRaster());
最后,更简单的方法是在新的空图像上绘制子图像。可能会稍微慢一些,因为它涉及渲染管道,但我认为它应该仍然可以合理地执行。
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create new empty image of same type
BufferedImage subThree = new BufferedImage(50, 50, image.getType());
// Draw the subimage onto the new, empty copy
Graphics2D g = subThree.createGraphics();
try {
g.drawImage(subimage, 0, 0, null);
}
finally {
g.dispose();
}
我想提取 BufferedImage 的矩形。
Javadoc 提出 getSubImage(x,y,w,h) 和 getData(rectangle)。
getData 很酷,但我不想要光栅。我希望子图像作为 BufferedImage 对象,但我还需要它的数据数组的修改版本,但 javadoc 说
public BufferedImage getSubimage(int x,int y,int w,int h) : Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
问:如何提取具有收缩数据数组的子图像?
很久以前我遇到过同样的问题,我不想要共享栅格。我找到的唯一解决方案是创建一个表示子图像的 BufferedImage,然后将像素复制到子图像中。
为了获得真正快速的东西,我直接访问 DataBuffer 并使用 System.arraycopy()
制作数组副本(逐行)给定一个 BufferedImage
图像,这里有 3 种创建 "deep" 复制子图像的方法:
// Create an image
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
// Fill with static
new Random().nextBytes(((DataBufferByte) image.getRaster().getDataBuffer()).getData());
围绕您从 getData(rect)
获得的 Raster
的已经很深的副本创建图像。这涉及转换为 WritableRaster
,因此它可能会与某些 Java 实现或将来中断。应该很快,因为你只复制一次数据:
// Get sub-raster, cast to writable and translate it to 0,0
WritableRaster data = ((WritableRaster) image.getData(new Rectangle(25, 25, 50, 50))).createWritableTranslatedChild(0, 0);
// Create new image with data
BufferedImage subOne = new BufferedImage(image.getColorModel(), data, image.isAlphaPremultiplied(), null);
另一个选项,创建子图像 "normal way",然后将光栅复制到新图像中。涉及创建一个子光栅,仍然只复制一次(并且没有投射):
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create empty compatible image
BufferedImage subTwo = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(50, 50), image.isAlphaPremultiplied(), null);
// Copy data into the new, empty image
subimage.copyData(subTwo.getRaster());
最后,更简单的方法是在新的空图像上绘制子图像。可能会稍微慢一些,因为它涉及渲染管道,但我认为它应该仍然可以合理地执行。
// Get subimage "normal way"
BufferedImage subimage = image.getSubimage(25, 25, 50, 50);
// Create new empty image of same type
BufferedImage subThree = new BufferedImage(50, 50, image.getType());
// Draw the subimage onto the new, empty copy
Graphics2D g = subThree.createGraphics();
try {
g.drawImage(subimage, 0, 0, null);
}
finally {
g.dispose();
}