1

伙计们!

我有一些问题。我需要从 jpg 图像中剪切一些多边形图像并保存。此时我使用 OpenSlideJNI.openslide_read_region,但 OpenSlide 可以剪切唯一的矩形。

你有什么主意吗?

4

1 回答 1

3

基本代码是:

// load the image

BufferedImage originalImage = ImageIO.read(...);

// create the polygon

Polygon polygon = new Polygon();
polygon.addPoint(50, 50);
polygon.addPoint(150, 50);
polygon.addPoint(250, 150);
polygon.addPoint(150, 150);

Rectangle bounds = polygon.getBounds();

// create a transparent clipped image based on the bounds of the Polygon

BufferedImage clippedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = clippedImage.createGraphics();

polygon.translate(-bounds.x, -bounds.y);
g.setClip(polygon);
g.drawImage(originalImage, -bounds.x, -bounds.y, null);

// save the clipped image

ImageIO.write(...);

当然,图像仍然是矩形的,但未裁剪的区域将是透明的。

于 2016-05-18T16:52:27.577 回答