java中的图像裁剪
我想在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要从 java.awt.Image 创建 java.awt.image.BufferedImage。以下是来自 DZone Snippets 的一些代码。
现在您有了 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.
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.
我不确定,但 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()