Java png Image Modification of Pixel rgba Value

  • 2021-12-12 04:30:55
  • OfStack

Directory Java png Image Modification Pixel rgba Value ARGB, RGB, RGBA Difference

Java png picture modifies pixel rgba value


import javax.imageio.ImageIO; 
import javax.swing.ImageIcon;
import java.awt.*; 
import java.awt.image.BufferedImage;
import java.io.File; 
public class  ReadColorTest { 
    public static void setAlpha(String os) {
                try {
                  ImageIcon imageIcon = new ImageIcon(os);
                  BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(),imageIcon.getIconHeight()
                      , BufferedImage.TYPE_4BYTE_ABGR);
                  Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
                  g2D.drawImage(imageIcon.getImage(), 0, 0,imageIcon.getImageObserver());
                  for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
                    for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
                      int pixel = bufferedImage.getRGB(j2, j1);//j2 Abscissa ,j1 Vertical coordinates 
                      int[]   rgb = new int[3];
                      rgb[0] = (pixel & 0x00ff0000) >> 16;// Bitwise and get red Then move to the right 
                      rgb[1] = (pixel & 0x0000ff00) >> 8;//.. Get green...
                      rgb[2] = (pixel & 0x000000ff);
                      int a=(pixel & 0xff000000)>>>24;// Unsigned right shift acquisition alpha Value 
                      if(comp(rgb[0],rgb[1],rgb[2])||a==0) {
                    	  pixel = pixel | 0xffffffff;// Transparent or inclined to white and shot to white 
                      }else {
                    	  pixel = (pixel & 0xff000000)| 0xff000000;// Otherwise it is black 
                      }
                      bufferedImage.setRGB(j2, j1, pixel);
                    }
                    System.out.println();
                  }
                  g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
                  ImageIO.write(bufferedImage, "png",  new File("D:\\2.png"));
                }
                catch (Exception e) {
                  e.printStackTrace();
                }
    }
    public static boolean comp(int r,int g,int b) {// Judge 2 Whether the value is black or white, true For white, false Be black 
    	int i = 0;
    	if(r>200) {
    		i++;
    	}
    	if(g>200) {
    		i++;
    	}
    	if(b>200) {
    		i++;
    	}
    	if(i>=2) {
    		return true;
    	}else {
    		return false;
    	}
    }
       
    public static void main(String[] args) throws Exception { 
        setAlpha("H:\\Test\\3.png");  
    } 
}

Differences between ARGB, RGB and RGBA

ARGB is a color mode, that is, RGB color mode with Alpha (transparency) channel, which is common in 32-bit bitmap storage structure. RGB color mode is a color standard in the industry, which obtains various colors by changing the three color channels of red (R), green (G) and blue (B) and superimposing them with each other. RGB is the color representing the three channels of red, green and blue. This standard almost includes all colors that human vision can perceive, and is one of the most widely used color systems at present. RGBA is the color space representing Red (red) Green (green) Blue (blue) and Alpha. Although it is sometimes described as a color space, it is only an additional information attached to the RGB model. The color used is RGB, which can belong to any RGB color space. However, Catmull and Smith put forward this indispensable alpha value between 1971 and 1972, which made alpha rendering and alpha synthesis possible. The author named it alpha after the classical linear interpolation equation α A + (1-α) B used this Greek letter.

PNG is an image format using RGBA.


Related articles: