One picture is better than six pictures, picture filter--java

We have learned before that all pictures presented on the screen are composed of colored pixels.

But now, given you an anime picture, how do you get the effect you want?

Gray out the color picture, add a mosaic to the picture, and achieve picture fusion?

Don't miss it if you pass by, all of these can be realized.

Now, we can give it a try.

1. Create a window class and add a filter button.

2. Create a new Fiter class to write our filter method.

Step 1: Write a method to obtain the information of the image (that is, the color of those pixels).

     public int[][] getpix(String path) {
        File file = new File(path);
        try {
            BufferedImage buf = ImageIO.read(file);
            int w = buf.getWidth();
            int h = buf.getHeight();
            int[][] imagearr = new int[w][h];
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    imagearr[i][j] = buf.getRGB(i, j);
                }
            }
            return imagearr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

A: First of all, we need to understand that after obtaining information, it needs to be stored, and what is more suitable than a two-dimensional array?

Therefore, we create a method that returns a two-dimensional array, which is convenient for later assignment operations.

The internal parameter (path) refers to the image path for later use.

B: After that, get the picture you want, and then extract the information.

Use the File class to get pictures. Then use the BufferedImage class to create a cache image and get the information of the image.

The purpose is to use the BufferedImage method to get the width, height, and RGB values.

Finally, use a two-dimensional array to store our information.

Step 2: Write the RGB value split method. (I mentioned it before, you can go and look it up, give me some views!!!)

Step 3: Write the graying method:

 public void begray(Graphics g) {
        int pix;
        int[][] img = getpix("C:\\Users\\27259\\Desktop\\jieyi.png");
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[i].length; j++) {
                pix = img[i][j];
                divide(pix);
                gray = (int) (0.4 * red + 0.25 * green + 0.35 * blue);
                Color cl = new Color(gray, gray, gray);
                g.setColor(cl);
                g.fillRect(300 + i, 300 + j, 1, 1);
            }
        }
    }//灰化

A: Create a two-dimensional array and use getpix(path) to get the picture information.

B: Traverse the array, color it, and draw a solid rectangle with a width and height of 1. (Pay attention to the gray = (int) (0.4 * red + 0.25 * green + 0.35 * blue) formula, the predecessors got it by hard work, is there any reason not to use it?)

 

Step 4: Write the mosaicization method:

 for (int i = 0; i < img.length; i += 5) {
            for (int j = 0; j < img[i].length; j += 5) {
                pix = img[i][j];
                Color cl=new Color(pix);
                g.setColor(cl);
                g.fillRect(300 + i , 300 + j , 5, 5);
            }
        }
 

A: Roughly the same as ashing.

B: Colors are only taken at intervals of a few points.

C: Draw a solid rectangle whose width and height are intervals.

 Step 5: Write the image fusion method:

A: Roughly the same as the graying method, but extract two pictures.

        int[][] img1 = getpix("C:\\Users\\27259\\Desktop\\jieyi.jpg");
        int[][] img2 = getpix("C:\\Users\\27259\\Desktop\\xuezhixia.png");

B: Pay attention to the difference in the width and height of the two pictures, and take a small value to prevent crossing the border.

C: Judge the pix size and decide to use the color of that picture.

3. Create a class (AC) to implement ActionListener, and then call the filter method in the Fiter class in the actionPerformed method.

4. You're done, go to work!

 

Guess you like

Origin blog.csdn.net/AkinanCZ/article/details/125222987