简体   繁体   English

循环遍历位图中的像素并添加简单的模糊

[英]Looping through pixels in bitmap and add simple blur

I have a bitmap which I want to loop through every pixel and add a simple blur effect, I just want the to take the average of the current pixel and it's 4/8 neighbors. 我有一个位图,我想遍历每个像素并添加一个简单的模糊效果,我只想获取当前像素的平均值及其4/8邻居。 I've seen some examples but most are fairly advanced and I'm looking for something really easy. 我已经看到了一些示例,但大多数示例都相当先进,我正在寻找一些非常简单的方法。

What I have so far is: 到目前为止,我有:

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

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

I have a method but i think it has a little of complexity. 我有一种方法,但我认为它有点复杂。 Don't worry I'll be here I'll try to make it clear for you =) 别担心,我会在这里,我会尽力为您弄清楚=)

1: So first of all you have to create a BufferedImage Object 1:首先,您必须创建一个BufferedImage对象

BufferedImage theImage = ImageIO.read(new File("pathOfTheImage.extension"));

2: Convert the BuferedImage into a int array. 2:将BuferedImage转换为一个int数组。 You should create a method to do that for you 您应该创建一个方法为您做到这一点

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: now you have an integer 1D array that contains all the pixels as a big concatenated integer, if you want to manipulate the colors separately, then you have to create 4 more methods : getRed(int) : int, getGreen(int) : int, getBlue(int) : int. 3:现在,您有了一个整数一维数组,其中包含所有像素作为一个较大的级联整数,如果要分别操纵颜色,则必须再创建4个方法:getRed(int):int,getGreen(int): int,getBlue(int):int。 those three methods give you the gradient of each color (0 ~ 255). 这三种方法为您提供每种颜色的渐变(0〜255)。 One last method makeRGB(int,int,int) : int. 最后一个方法makeRGB(int,int,int):int。 This method create a pixels from the RGB colors component. 此方法从RGB颜色分量创建像素。 This is the core of every method ^^ : 这是每种方法^^的核心:

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

4: last thing to talk about is how to turn the int array into a BufferedImage again. 4:最后要谈的是如何将int数组再次转换为BufferedImage。 This is the code to make it ;) 这是实现它的代码;)

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;

}

I hope that helps you, Salam 希望对您有帮助,萨拉姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM