Java 删除BuffereImage像素值并将其设置为透明
我一直在使用polygon类,并试图将多边形内部的像素值设置为透明,或者如果可能的话将它们全部删除,但是我在尝试将这些值存储为RGB int值时遇到了一些困难,我不知道如何通过这种方法使像素透明/删除 除此之外,我还想做同样的事情,但保留多边形内的像素,如果可能的话,删除那些外部的像素,以便只保留多边形内包含的像素。我以前到处找过这个,但没有用 我曾尝试为此创建一个SSCCE,以使任何人都能更轻松地与之合作并查看,但作为我正在创建的更大计划的一部分,创建一个SSCCE需要一些时间,但一旦我有了一个SSCCE,以更好地证明这个问题,我将编辑此帖子 谢谢大家花时间帮我解决这个问题 下面我有一些代码,用于我当前用于分割包含在已指定多边形中的像素。这与我将多边形外的像素设置为透明的方式极为相似,仅使用if语句参数交换,以删除图像的一部分,并返回新图像,而不是保存图像内容,效果非常好,但是,当我用这种方式保存多边形中包含的像素时,由于某种原因它不会保存Java 删除BuffereImage像素值并将其设置为透明,java,polygon,bufferedimage,alpha,pixels,Java,Polygon,Bufferedimage,Alpha,Pixels,我一直在使用polygon类,并试图将多边形内部的像素值设置为透明,或者如果可能的话将它们全部删除,但是我在尝试将这些值存储为RGB int值时遇到了一些困难,我不知道如何通过这种方法使像素透明/删除 除此之外,我还想做同样的事情,但保留多边形内的像素,如果可能的话,删除那些外部的像素,以便只保留多边形内包含的像素。我以前到处找过这个,但没有用 我曾尝试为此创建一个SSCCE,以使任何人都能更轻松地与之合作并查看,但作为我正在创建的更大计划的一部分,创建一个SSCCE需要一些时间,但一旦我有了一
public void saveSegment(int tabNum, BufferedImage img) {
segmentation = new GUI.Segmentation();
Polygon p = new Polygon();
Color pixel;
p = createPolygon(segmentation);
int height = img.getHeight();
int width = img.getWidth();
newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//loop through the image to fill the 2d array up with the segmented pixels
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
//If the pixel is inside polygon
if(p.contains(x, y) == true) {
pixel = new Color(img.getRGB(x, y));
//set pixel equal to the RGB value of the pixel being looked at
int r = pixel.getRed(); // red component 0...255
int g = pixel.getGreen(); // green component 0...255
int b = pixel.getBlue(); // blue component 0...255
int a = pixel.getAlpha(); // alpha (transparency) component 0...255
int col = (a << 24) | (r << 16) | (g << 8) | b;
newImage.setRGB(x, y, col);
}
else {
pixel = new Color(img.getRGB(x, y));
int a = 0; // alpha (transparency) component 0...255
int col = (a << 24);
newImage.setRGB(x, y, col);
}
}
}
try {
//then save as image once all in correct order
ImageIO.write(newImage, "bmp", new File("saved-Segment.bmp"));
JOptionPane.showMessageDialog(null, "New image saved successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
public void存储段(int tabNum,buffereImage img){
segmentation=newgui.segmentation();
多边形p=新多边形();
彩色像素;
p=创建多边形(分段);
int height=img.getHeight();
int width=img.getWidth();
newImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_ARGB);
//在图像中循环,用分割的像素填充2d阵列
对于(int y=0;y int col=(a更简单的方法是使用Java2D的剪裁功能:
BufferedImage cutHole(BufferedImage image, Polygon holeShape) {
BufferedImage newImage = new BufferedImage(
image.getWidth(), image.getHeight(), image.getType());
Graphics2D g = newImage.createGraphics();
Rectangle entireImage =
new Rectangle(image.getWidth(), image.getHeight());
Area clip = new Area(entireImage);
clip.subtract(new Area(holeShape));
g.clip(clip);
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
BufferedImage clipToPolygon(BufferedImage image, Polygon polygon) {
BufferedImage newImage = new BufferedImage(
image.getWidth(), image.getHeight(), image.getType());
Graphics2D g = newImage.createGraphics();
g.clip(polygon);
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
首先,没有定义null pointer as img。另外,else后面的if语句不需要,因为它始终是真的。感谢这些指针。添加缓冲图像是为了帮助显示我在该线程中的问题,所以我必须查看过去。我已经更新了该问题,以便现在更好地显示我的问题。谢谢!