我试图在Java中获取RenderedImage的子部分。举例来说,图片为100x100,我想要该图片的右下角,

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

RenderedImage i2=...

最佳答案

如果您的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);

关于java - Java RenderedImage小节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24991286/

10-10 18:40