Java RenderedImage子部分

时间:2014-07-28 08:35:56

标签: java image-processing

我试图在java中得到一个RenderedImage的子部分。比如说图像是100x100,我想要那个图像的右下角

RenderedImage i=...
    x=49;
    y=49;
    width=50;
    height=50;

RenderedImage i2=...

1 个答案:

答案 0 :(得分:0)

如果您的RenderedImageBufferedImage,则可以使用getSubImage()方法:

BufferedImage bi = ...;
BufferedImage bi2 = bi.getSubImage(x, y, width, height);

注意:bi2将与bi共享图像数据数组。

如果您的RenderedImage不是BufferedImage,则必须执行更严格的操作"方式:

WritableRaster raster = i.getData(new Rectangle(x, y, width, height))
            .createCompatibleWritableRaster();
Hashtable<String,Object> properties = new Hashtable<String,Object>();
for (String name : i.getPropertyNames())
    properties.put(name,i.getProperty(name));

// And finally creating a BufferedImage
// which of course implements RenderedImage:
RenderedImage i2 = new BufferedImage(i.getColorModel(), raster,
       i.getColorModel().isAlphaPremultiplied(), properties);