Java 在位图中循环像素并添加简单的模糊

Java 在位图中循环像素并添加简单的模糊,java,android,Java,Android,我有一个位图,我想循环通过每个像素,并添加一个简单的模糊效果,我只想取当前像素的平均值和它的4/8邻居。我看过一些例子,但大多数都相当先进,我正在寻找一些非常简单的东西 到目前为止,我得到的是: int height = mPhoto.getHeight(); int width = mPhoto.getWidth(); int[] pixels = new int[height*width]; mPhoto.getPixels(pixels, 0, 0, 0, 0, height, widt

我有一个位图,我想循环通过每个像素,并添加一个简单的模糊效果,我只想取当前像素的平均值和它的4/8邻居。我看过一些例子,但大多数都相当先进,我正在寻找一些非常简单的东西

到目前为止,我得到的是:

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

int[] pixels = new int[height*width];
mPhoto.getPixels(pixels, 0, 0, 0, 0, height, width);

我有一个方法,但我认为它有点复杂。别担心,我会在这里的,我会尽力让你明白的=)

1:所以首先你必须创建一个BuffereImage对象

BufferedImage theImage = ImageIO.read(new File("pathOfTheImage.extension"));
2:将BuferedImage转换为int数组。您应该创建一个方法来为您做到这一点

public static int[] rasterize(BufferedImage img) {
        int[] pixels = new int[img.getWidth() * img.getHeight()];
        PixelGrabber grabber = new PixelGrabber(img, 0, 0, img.getWidth(),
                img.getHeight(), pixels, 0, img.getWidth());
        try {
            grabber.grabPixels();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return pixels;
    }
3:现在你有了一个整数1D数组,它包含所有的像素作为一个大的串联整数,如果你想单独操作颜色,那么你必须再创建4个方法:getRed(int):int,getGreen(int):int,getBlue(int):int。这三个方法给你每种颜色的渐变(0~255)。最后一个方法makeRGB(int,int,int):int。这个方法从RGB颜色组件创建像素。 这是每个方法的核心^^^:

public static int getRed(int RGB) {
    return (RGB  >> 16) & 0xff;
}

public static int getGreen(int RGB) {
    return (RGB  >> 8) & 0xff;
}

public static int getBlue(int RGB) {
    return RGB & 0xff;
}
public static int makeRGB(int red, int green, int blue) {
    return ((red << 16) & 0xff) + ((green << 8) & 0xff) + (blue & 0xff); 
}

我希望这对你有帮助,Salam

你是否只是尝试过缩小图像大小,然后再放大图像以获得模糊效果?如果没有的话,我会发布一些代码。我有,它工作得不错,但我想尝试制作一些不同的图像过滤器,第一步似乎是学习/理解如何用这种方式制作一个非常简单的模糊。
public static BufferedImage arrayToBufferedImage(int[] array, int w, int h) {

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = (WritableRaster) image.getData();
    raster.setPixel(0, 0, array);

    return image;

}