如何在Java中清除BufferedImage的像素

如何在Java中清除BufferedImage的像素

本文介绍了如何在Java中清除BufferedImage的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我读取图像,然后遍历像素,并且其颜色距离是否小于<. 30,然后我想通过将其alpha更改为0来清除图像.这是我的代码:

In java, I read an image and then go through the pixels and if its color distance is < 30, then I want to clear the image by changing its alpha to 0. This is my code:

但是这不起作用.它没有任何作用...

But this is not working. It is having no effect...

有人看到这个问题吗?

谢谢

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);
    }
}

推荐答案

通过清除"您显然意味着使像素透明"的像素.

By "clearing" the pixel you obviously meant "to make the pixel transparent".

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

In order to be able to make a pixel transparent, the image has to support transparent pixels. Whether or not a BufferedImage supports transparent pixels depends on the type of the BufferedImage. After loading a BufferedImage with ImageIO, you hardly know the type of the image. But you can easily convert the image into an image with a known type (that supports transparency) by passing it to a method like this:

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;
}

这篇关于如何在Java中清除BufferedImage的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:00