Home > Java > javaTutorial > java loads images, displays images and grayscales images

java loads images, displays images and grayscales images

黄舟
Release: 2016-12-30 11:58:35
Original
1663 people have browsed it

The java basic package provides image classes. The ones we commonly use include java.awt.image.BufferedImage, javax.imageio.ImageIO, etc. In fact, these two classes are enough. The former is about basic operations on images, and the latter is about reading images.

Load image:

      BufferedImage img = null;  
        try{  
            img = ImageIO.read(new FileInputStream("/home/eple/DIP/o.jpg"));  
          
        }catch (IOException e) {  
            //e.printStackTrace();  
        }
Copy after login

I am running under ubuntu, so the file path is slightly different from that of windows.

In order to make the code more versatile, I created a new image processing class Imgae.

public class Image{
	
	public int h; //高
	public int w; //宽
	public int[] data; //像素
	public boolean gray; //是否为灰度图像
	
	public Image(BufferedImage img){
		
		this.h = img.getHeight();
		this.w = img.getWidth();
		
		this.data = img.getRGB(0, 0, w, h, null, 0, w);
		this.gray = false;
		toGray(); //灰度化
	}
	
	public Image(BufferedImage img,int gray){
		
		this.h = img.getHeight();
		this.w = img.getWidth();
		this.data = img.getRGB(0, 0, w, h, null, 0, w);
		this.gray = false;

	}

	public Image(int[] data,int h,int w){
		this.data = (data == null) ? new int[w*h]:data;
		this.h = h;
		this.w = w;
		this.gray = false;
	}
	
	public Image(int h,int w){
		this(null,h,w);
	}

        public BufferedImage toImage(){
               BufferedImage image = new BufferedImage(this.w, this.h, BufferedImage.TYPE_INT_ARGB);
               int[] d= new int[w*h];
               for(int i=0;i<this.h;i++){
               for(int j=0;j<this.w;j++){
                   if(this.gray){
                       d[j+i*this.w] = (255<<24)|(data[j+i*this.w]<<16)|(data[j+i*this.w]<<8)|(data[j+i*this.w]);
                   }else{
                       d[j+i*this.w] = data[j+i*this.w];
                   }
               }
         }
            image.setRGB( 0, 0, w, h, d, 0, w ); 
            return image;
     }

}
Copy after login

In this way, we do not rely on the methods provided by java itself. We only need to convert the image into the class we define. For example, in Android, the reading and survival of pixels are as follows, just replace the relevant functions.

 //android 中的获取方式RGB分量    
   pixelsA = Color.alpha(color);    
   pixelsR = Color.red(color);    
   pixelsG = Color.green(color);    
   pixelsB = Color.blue(color);  
                      
 // 根据新的RGB生成新像素    
   newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);
Copy after login


Okay, the above class contains methods for reading image pixel information, saving it as an int array, and converting the int array into a Bufferedmage. Next we will talk about how to decolorize images, that is, grayscale.

We mentioned in the previous article that regarding the fact of image processing, we are more concerned about the brightness information of the image, that is, grayscale. How to convert a color image into a grayscale image? It's very simple, just make the three values ​​​​of R, G, and B equal. So what is the relationship between this value and the original values ​​of R, G, and B?

Generally, we have the empirical formula Gray=R×0.299+G×0.587+B×0.114, or just take their average value directly. The previous empirical formula is more consistent with the observation of the human eye. The grayscale function is as follows:

public void toGray(){  
          
        if(!gray){  
            this.gray = true;  
            for (int y = 0; y < h; y++) {  
                       for (int x = 0; x < w; x++) {  
                          int c = this.data[x + y * w];  
                          int R = (c >> 16) & 0xFF;  
                          int G = (c >> 8) & 0xFF;  
                          int B = (c >> 0) & 0xFF;  
                          this.data[x + y * w] = (int)(0.3f*R + 0.59f*G + 0.11f*B); //to gray  
                }  
              }  
        }  
    }
Copy after login

The processing effect is as follows:

java loads images, displays images and grayscales images

java loads images, displays images and grayscales images

## The above is java loading the image, displaying the image and The grayscale content of the image. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template