如何在java中清除BuffereImage的像素?

如何在java中清除BuffereImage的像素?,java,pixel,bufferedimage,alpha,Java,Pixel,Bufferedimage,Alpha,在java中,我读取一个图像,然后遍历像素,如果它的颜色距离小于30,那么我希望通过将其alpha更改为0来清除图像。这是我的代码: 但这是行不通的。这是没有效果的 有人看到问题了吗 谢谢 import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.fi

在java中,我读取一个图像,然后遍历像素,如果它的颜色距离小于30,那么我希望通过将其alpha更改为0来清除图像。这是我的代码:

但这是行不通的。这是没有效果的

有人看到问题了吗

谢谢

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;

public class Recognize {

    public static void main(String args[]) throws IOException {

        Path path = Paths.get("images/fish.png");
        File file = path.toFile();

        if (file.exists()) {
            InputStream stream = Files.newInputStream(path);
            BufferedImage bufferedImage = ImageIO.read(stream);

            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            if (width > 0 && height > 0) {
                int TLpixel = bufferedImage.getRGB(0, 0);
                Color TLcolor = new Color(TLpixel);

                for (int i = 0; i < width; i++) {
                    for (int j = 0; j < height; j++) {
                        int pixel = bufferedImage.getRGB(i, j);
                        Color color = new Color(pixel);
                        double distance = ColourDistance(TLcolor, color);
                        //System.out.println(distance);

                        if (distance < 30) {
                            int mc = (0 << 24) | 0x00ffffff;
                            int newcolor = pixel & mc;
                            bufferedImage.setRGB(i, j, newcolor);      
                        }
                    } 
                }



                File outputfile = new File("images/fish_new.png");
                ImageIO.write(bufferedImage, "png", outputfile);

            }
        }
    }

    public static int[] printPixelARGB(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        return new int[] {red, green, blue, alpha};
    }

    public static double ColourDistance(Color c1, Color c2) {
        double rmean = ( c1.getRed() + c2.getRed() )/2;
        int r = c1.getRed() - c2.getRed();
        int g = c1.getGreen() - c2.getGreen();
        int b = c1.getBlue() - c2.getBlue();
        double weightR = 2 + rmean/256;
        double weightG = 4.0;
        double weightB = 2 + (255-rmean)/256;
        return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
    } 
}
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.io.InputStream;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入javax.imageio.imageio;
公众阶级承认{
公共静态void main(字符串args[])引发IOException{
Path=Path.get(“images/fish.png”);
File File=path.toFile();
if(file.exists()){
InputStream=Files.newInputStream(路径);
BuffereImage BuffereImage=ImageIO.read(流);
int width=bufferedImage.getWidth();
int height=bufferedImage.getHeight();
如果(宽度>0和高度>0){
int TLpixel=bufferedImage.getRGB(0,0);
颜色TLcolor=新颜色(TLpixel);
对于(int i=0;i24)&0xff;
int red=(像素>>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
返回新的int[]{red,green,blue,alpha};
}
公共静态双色距(颜色c1、颜色c2){
双rmean=(c1.getRed()+c2.getRed())/2;
int r=c1.getRed()-c2.getRed();
int g=c1.getGreen()-c2.getGreen();
intb=c1.getBlue()-c2.getBlue();
双权重R=2+rmean/256;
双权重g=4.0;
双权重B=2+(255 rmean)/256;
返回Math.sqrt(weightR*r*r+weightG*g*g+weightB*b*b);
} 
}
通过“清除”像素,您的意思显然是“使像素透明”

为了使像素透明,图像必须支持透明像素。
BufferedImage
是否支持透明像素取决于
BufferedImage
的类型。使用
ImageIO
加载
BufferedImage
后,您几乎不知道图像的类型。但您可以轻松地通过将图像传递给以下方法,将其转换为具有已知类型(支持透明度)的图像:

public static BufferedImage convertToARGB(BufferedImage image)
{
    BufferedImage newImage = new BufferedImage(
        image.getWidth(), image.getHeight(),
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = newImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return newImage;
}

你说不工作是什么意思?试着打印
buffereImage
的值。你确定它包含alpha通道吗?不是所有类型的图像都支持透明度,如果不支持,那么将alpha设置为
0
很可能没有任何效果。作为haraldK提示的旁注
0+1:你几乎不知道图像。您可以尝试将图像转换为明确包含alpha通道的图像,例如使用@user…(用户…)中的
convertToARGB
:其工作方式与中不同,当我看到输出文件时,它看起来与原始文件完全相同。我希望某些像素是透明的,但它不是。@Marco13:感谢链接中的解决方案od。(我指的是将函数转换为RGB)