Java blur Image

后端 未结 7 1714
臣服心动
臣服心动 2021-01-11 17:48

I am trying to blur the image

   int radius = 11;
    int size = radius * 2 + 1;
    float weight = 1.0f / (size * size);
    float[] data = new float[size *         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 18:17

    I always do something like this:

    public BufferedImage diagonalBlur(int range, int angle)
        {
            BufferedImage b = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = b.createGraphics();
            
            for(int x = 0; x < main_image.getWidth(); x++)
            {
                for(int y = 0; y < main_image.getHeight(); y++)
                    
                    int red[] = new int[range * 2], green[] = new int[range * 2], blue[] = new int[range * 2];
                    int pixels[] = new int[range * 2];
                    
                    for(int i = 0; i < pixels.length; i++)
                    {
                        pixels[i] = main_image.getRGB(clamp(x - clamp(range / 2, 0, range) + i, 0, main_image.getWidth() - 1), clamp(y - clamp(range / 2, 0, range) + (int)(i * Math.toRadians(angle)), 0, main_image.getHeight() - 1));
                        
                        red[i] = (pixels[i] >> 16) & 0xff;
                        green[i] = (pixels[i] >> 8) & 0xff;
                        blue[i] = (pixels[i]) & 0xff;
                    }
                    
                    int red_t = 0, green_t = 0, blue_t = 0;
                    
                    for(int i = 0; i < pixels.length; i++)
                    {
                        red_t += red[i];
                        green_t += green[i];
                        blue_t += blue[i];
                    }
                    
                    int r = red_t / (range * 2);
                    int gr = green_t / (range * 2);
                    int bl = blue_t / (range * 2);
                    
                    //System.out.println(r + ", " + gr + ", " + bl);
                    
                    g.setColor(new Color(r, gr, bl));
                    g.fillRect(x, y, 1, 1);
                    
                }
            }
            g.dispose();
            
            return b;
    

    then, doing something along the lines of:

    public static void main(String a[])
    {
      File f = new File("path");
      try{
        ImageIO.write(diagonalBlur(10, 69), "png", f);
      }
      catch(IOException e)
      {
        e.printStackTrace();
      }
    }
    }
    

    That should save the file as a BufferedImage. of course, you need a reference to an image. In the code I used main_image, but that was a variable I created earlier:

    public BufferedImage main_image;
    

    to initiate it I used

    try
    {
      main_image = ImageIO.read(new File("path"));
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    

    in the main method. Adding a little bit of JFrame code, allowed me to do this: blur If you want to have a gaussian blur, you just have to make the red, green, blue and pixels variable a 2-dimensional array and repeat the proccess for the y-axis. I hope I could help.

提交回复
热议问题