Beauty camera (realization of various filter effects in java)

Posted by jateeq on Tue, 15 Feb 2022 10:48:26 +0100

1. Painting of normal pictures

	//Function: normal picture painting
    public void drawzc(int arr[][],Graphics g) 
    {
    	BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_RGB);
        Graphics ng =bff.getGraphics();
    	for(int i=0;i<arr.length;i++) 
    	{
    		for(int j=0;j<arr[0].length;j++) 
    		{
    			int pixel =arr[i][j];
    			//bff.setRGB(i, j, pixel);
    			//Will (0101010101010101...) The number in this binary mode is converted into the form of (r,g,b)
    			
    			Color color =new Color(pixel);
    			//Painting point by point will make painting very slow
    			ng.setColor(color);
				ng.drawLine(i, j, i, j);
    		}
    	}
		g.drawImage(bff, 0, 0,null);
    }

1.1 understanding of buffer picture and buffer

1: If you don't use a class like BufferedImage, the picture will slowly appear on the screen
When using the buffer, we first draw various pixels of the picture in the buffer, and finally transmit them to the computer screen at one time. In that case, the picture itself will appear on the computer at one time
2: We can understand this process because the sequence of picture pixels is cyclic. If we draw directly, it is equivalent to a little bit of data transmission. However, when we use the buffer, it is equivalent to storing a certain amount of data at the transmission port. When a certain value is reached, we will transfer all the overall data to the computer
3: It is equivalent to directly using the faucet to connect water to the water tank, or first storing a certain amount of water in a bucket, and then directly connecting water to the water tank

1.2 function of DrawImage

g. DrawImage (parameter 1: buffer picture, parameter 2: x coordinate of picture, parameter 3: y coordinate of picture, null)
Function: present the picture painted on the buffer on the screen

1.3 normal picture presentation

2. Mosaic drawing

    public void drawMsk(int arr[][],Graphics g) 
    {
        BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_RGB);
        Graphics ng =bff.getGraphics();
		//Draw the mosaic matrix
		for(int i=0;i<arr.length;i+=10) 
		{
			for(intms j=0;j<arr[0].length;j+=10) 
			{
				int pixel =arr[i][j];
				Color color =new Color(pixel);
				//Because where the brush is obtained, its painting will be carried out
				//Painting with the pixels obtained from the buffer picture is equivalent to painting all the buffer pictures
				ng.setColor(color);
				ng.fillRect(i, j, 10, 10);
			}
		}
		g.drawImage(bff, 0,0,null);
    }

2.1 explanation of mosaic drawing principle

The implementation of mosaic can be understood as that we take a pixel every ten points on the wide and high picture pixels, and then use the value of the pixel as the value of 100 pixel positions, using ng FillRect (I, J, 10,10)
So that the following figure is the value of the pixel in the upper left corner every time to fill the position of 100 pixels

2.2 presentation of mosaic pictures

3. Drawing of gray image

    public void drawhd(int arr[][],Graphics g) 
    {
        BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_RGB);
        Graphics ng =bff.getGraphics();
    	for(int i=0;i<arr.length;i++) 
    	{
    		for(int j=0;j<arr[0].length;j++) 
    		{
    			int pixel =arr[i][j];
    			Color color =new Color(pixel);
    			//(r,g,b)
    			int red =(int)(color.getRed()*0.299);
    			int green =(int)(color.getGreen()*0.587);
    			int blue =(int)(color.getBlue()*0.114);
    			Color newcolor =new Color(red+green+blue,red+green+blue,red+green+blue);
    			ng.setColor(newcolor);//What you get is
    			ng.drawLine(i, j, i, j);
    		}
    	}
    	g.drawImage(bff,0,0,null);
    }

3.1 explanation of gray scale principle

When you take a certain coefficient from the rgb value in the picture to obtain the values of red, green and blue, and finally add them to the new color option
int red =(int)(color.getRed()*0.299);
int green =(int)(color.getGreen()*0.587);
int blue =(int)(color.getBlue()*0.114);
Color newcolor =new Color(red+green+blue,red+green+blue,red+green+blue)

3.2 grayscale picture effect presentation

4. Drawing of negative effect

    public void drawdp(int arr[][],Graphics g) 
    {
    	BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_BGR);
    	Graphics ng = bff.getGraphics();
    	
    	for (int i=0;i<arr.length;i++) 
    	{
    		for (int j=0;j<arr[0].length;j++) 
    		{
    			int pixel =arr[i][j];
    			int newpixel =255-pixel;
    			Color color =new Color(newpixel);
    			ng.setColor(color);
    			ng.drawLine(i, j, i,j);
    		}
    	}
    	g.drawImage(bff, 0, 0,null);
    }

4.1 principle of negative effect drawing

			int pixel =arr[i][j];
			int newpixel =255-pixel;
			Color color =new Color(newpixel);

4.2 negative effect picture display

5. Oil painting effect

public void drawyh(int arr[][],Graphics g) 
{
	BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_BGR);
	Graphics ng =bff.getGraphics();
	for(int i=0;i<arr.length;i++) 
	{
		for(int j=0;j<arr[0].length;j++) 
		{
			int pixel =arr[i][j];
			Color color =new Color(pixel);
			ng.setColor(color);
			Random random =new Random();
			//This line of code means to take a number below 20 and add 5
			int r =random.nextInt(20)+10;
			ng.fillOval(i, j, r, r);
		}
	}
	g.drawImage(bff, 0, 0, null);
}

5.1 oil painting principle

			Random random =new Random();
			//This line of code means to take a number below 20 and add 5
			int r =random.nextInt(20)+10;
			ng.fillOval(i, j, r, r);
			Create a random Example, get an integer below 20, then add 10, and finally i,j On the position of the, draw r*r Size ellipse

5.2 oil painting effect display

6. Realization of black-and-white effect

    public void black_white(int arr[][],Graphics g) 
    {
    	BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_BGR);
    	Graphics ng =bff.getGraphics();
    	for(int i=0;i<arr.length;i++) 
    	{
    		for (int j=0;j<arr[0].length;j++) 
    		{
    			int pixel =arr[i][j];
    			Color color =new Color(pixel);
    			int r=color.getRed();
    			int green=color.getGreen();
    			int b =color.getBlue();
    			int val =(r+green+b)/3;
    			if(val>100) 
    			{
    				ng.setColor(Color.BLACK);
    			}
    			else 
    			{
    				ng.setColor(Color.white);
    			}
    			ng.drawLine(i, j, i, j);
    		}
    	}
    	g.drawImage(bff, 0, 0,null);
    }

6.1 realization principle of black-and-white effect

			int pixel =arr[i][j];
			Color color =new Color(pixel);
			int r=color.getRed();
			int green=color.getGreen();
			int b =color.getBlue();
			int val =(r+green+b)/3;
			if(val>100) 
			{
				ng.setColor(Color.BLACK);
			}
			else 
			{
				ng.setColor(Color.white);
			}
			ng.drawLine(i, j, i, j);
		}

That is, when the added value of R, G and B / 3 is greater than 100, we understand that the pixel of this point is a brighter point, so we draw it as white, otherwise we set it as black

6.2 black and white picture display

7. Realization of bead effect

    public void drawzw(int arr[][],Graphics g) 
    {
        BufferedImage bff =new BufferedImage(arr.length,arr[0].length,BufferedImage.TYPE_INT_RGB);
        Graphics ng =bff.getGraphics();
    	for(int i=0;i<arr.length;i+=10) 
    	{
    		for(int j=0;j<arr[0].length;j+=10) 
    		{
    			int pixel =arr[i][j];
    			Color color =new Color(pixel);
    			ng.setColor(color);
    			ng.fillOval(i, j, 8, 8);
    		}
    	}
		g.drawImage(bff, 0, 0,null);
    }

7.1 implementation principle of bead pattern

	for(int i=0;i<arr.length;i+=10) 
	{
		for(int j=0;j<arr[0].length;j+=10) 
		{
			int pixel =arr[i][j];
			Color color =new Color(pixel);
			ng.setColor(color);
			ng.fillOval(i, j, 8, 8);
		}
	}

7.2 bead pattern picture display

8. Realization of composite picture

    public void compound(int arr1[][],int arr2[][],Graphics g) 
    {
    	BufferedImage bff =new BufferedImage(arr1.length,arr1[0].length,BufferedImage.TYPE_INT_BGR);
    	Graphics ng =bff.getGraphics();
    	for(int i=0;i<arr1.length;i++) 
    	{
    		for(int j=0;j<arr1[0].length;j++) 
    		{
    			if(arr2.length>i && arr2[0].length>j)
    			{
    			int pixel1=arr1[i][j];
    			int pixel2=arr2[i][j];
    			Color color1 =new Color(pixel1);
    			Color color2 =new Color(pixel2);
    			int r1=color1.getRed();
    			int r2=color2.getRed();
    			int r=(int)(r1*0.7+r2*0.3);
    			int g1=color1.getGreen();
    			int g2=color2.getGreen();
    			int green=(int)(g1*0.7+g2*0.3);
    			int b1=color1.getBlue();
    			int b2=color1.getBlue();
    			int b=(int)(b1*0.7+b2*0.3);
    			Color newcolor =new Color(r,green,b);
    			ng.setColor(newcolor);
    			ng.drawLine(i, j, i, j);
    			}
    			else 
    			{
    				int pixel1 =arr1[i][j];
    				Color newcolor= new Color(pixel1);
    				ng.setColor(newcolor);
    				ng.drawLine(i, j, i, j);
    			}
    		}
    	}
    	g.drawImage(bff, 50, 50,null);
    }

8.1 principle of composite picture

		Color color1 =new Color(pixel1);
			Color color2 =new Color(pixel2);
			int r1=color1.getRed();
			int r2=color2.getRed();
			int r=(int)(r1*0.7+r2*0.3);
			int g1=color1.getGreen();
			int g2=color2.getGreen();
			int green=(int)(g1*0.7+g2*0.3);
			int b1=color1.getBlue();
			int b2=color1.getBlue();
			int b=(int)(b1*0.7+b2*0.3);

In fact, after obtaining the pixel matrix of two pictures, multiply their rgb values by a certain scale coefficient, then add and assign them to the new rgb values, and finally draw them, then a composite picture will be formed

8.2 display of composite pictures

Topics: Java Back-end