java中的图像裁剪

发布于 2024-09-06 20:14:58 字数 117 浏览 7 评论 0原文

我想在java中剪切图像的特定形状,例如包含白色背景的男人的图像,这里我想裁剪没有背景的男人。不想将其设为透明图像,想用一些坐标进行剪切。我认为使用cropImageFilter我们只能剪切矩形区域。谁能告诉我该怎么做?

I want to cut a particular shape of an image in java, for example an image which contains a man with white background, here I want to crop the man without the background. Don't want to make it as transparent image, want to cut with some coordinates. I think using the cropImageFilter we can only cut the rectangle region. Can anyone tel me how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-09-13 20:14:58

首先,您需要从 java.awt.Image 创建 java.awt.image.BufferedImage。以下是来自 DZone Snippets 的一些代码。

/**
 * @author Anthony Eden
 */
public class BufferedImageBuilder {

    private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;

    public BufferedImage bufferImage(Image image) {
        return bufferImage(image, DEFAULT_IMAGE_TYPE);
    }

    public BufferedImage bufferImage(Image image, int type) {
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(image, null, null);
        waitForImage(bufferedImage);
        return bufferedImage;
    }

    private void waitForImage(BufferedImage bufferedImage) {
        final ImageLoadStatus imageLoadStatus = new ImageLoadStatus();
        bufferedImage.getHeight(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.heightDone = true;
                    return true;
                }
                return false;
            }
        });
        bufferedImage.getWidth(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.widthDone = true;
                    return true;
                }
                return false;
            }
        });
        while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {

            }
        }
    }

    class ImageLoadStatus {

        public boolean widthDone = false;
        public boolean heightDone = false;
    }

}

现在您有了 BufferedImage,您可以使用该多边形坐标,您必须将不是人的像素变成透明。只需使用 BufferedImage 中提供的方法即可。

您无法从字面上从 BufferedImage 中剪切出多边形。 BufferedImage 必须是矩形。您能做的最好的事情就是使图像中不需要的部分变得透明。或者,您可以将所需的像素放在另一个矩形 BufferedImage 上。

First, you need to create a java.awt.image.BufferedImage from a java.awt.Image. Here's some code to do that, from DZone Snippets.

/**
 * @author Anthony Eden
 */
public class BufferedImageBuilder {

    private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;

    public BufferedImage bufferImage(Image image) {
        return bufferImage(image, DEFAULT_IMAGE_TYPE);
    }

    public BufferedImage bufferImage(Image image, int type) {
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        Graphics2D g = bufferedImage.createGraphics();
        g.drawImage(image, null, null);
        waitForImage(bufferedImage);
        return bufferedImage;
    }

    private void waitForImage(BufferedImage bufferedImage) {
        final ImageLoadStatus imageLoadStatus = new ImageLoadStatus();
        bufferedImage.getHeight(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.heightDone = true;
                    return true;
                }
                return false;
            }
        });
        bufferedImage.getWidth(new ImageObserver() {
            public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if (infoflags == ALLBITS) {
                    imageLoadStatus.widthDone = true;
                    return true;
                }
                return false;
            }
        });
        while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {

            }
        }
    }

    class ImageLoadStatus {

        public boolean widthDone = false;
        public boolean heightDone = false;
    }

}

Now that you have a BufferedImage, you can use that polygon of coordinates you have to turn the pixels that are not the man, transparent. Just use the methods provided in BufferedImage.

You can't literally cut a polygon from a BufferedImage. A BufferedImage has to be a rectangle. The best you can do is make the parts of the image you don't want transparent. Or, you can put the pixels you do want on another rectangular BufferedImage.

这样的小城市 2024-09-13 20:14:58

我不确定,但 Graphics2D 类有一个方法 clip() 接受多边形,我认为可以满足您的需要。

因此,从您的图像创建一个 BufferedImage,并使用 createGraphics() 获取 Graphics2D 对象

I'm not sure but class Graphics2D has amethod clip() that accepts a polygon and I think does what you need.

So create a BufferedImage from your image, and get Graphics2D object with createGraphics()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文