我试图在java中得到一个RenderedImage的子部分。比如说图像是100x100,我想要那个图像的右下角
RenderedImage i=...
x=49;
y=49;
width=50;
height=50;
RenderedImage i2=...
答案 0 :(得分:0)
如果您的RenderedImage
是BufferedImage,则可以使用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);