I don’t have a deep understanding of the content of Java image processing, so I’ll forgive you for your simple words.
Java implements color level adjustment, that is, adjusts the rgb component of the picture, and can also adjust the brightness of the picture.
Test code
public static void main(String[] args) {//Conversion between file and BufferedImage BufferedImage bi=file2img("test.jpg");//Read the image BufferedImage bii=img_color_gradation(bi,100,0,0);img2file(bii,"jpg","test1.jpg");//Generate the image}Color level adjustment code
//Adjust the image color level, adjust the component of rgb public static BufferedImage img_color_gradation(BufferedImage imgsrc, int r, int g, int b) { try { //Create an image without transparency BufferedImage back=new BufferedImage(imgsrc.getWidth(), imgsrc.getHeight(),BufferedImage.TYPE_INT_RGB); int width = imgsrc.getWidth(); int height = imgsrc.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int pixel = imgsrc.getRGB(j, i); Color color = new Color(pixel); int red= color.getRed()+r; if(red>255) red=255; if(red<0) red=0; int green= color.getGreen()+g; if(green>255) green=255; if(green<0) green=0; int blue= color.getBlue()+b; if(blue>255) blue=255; if(blue<0) blue=0; color = new Color(red,green,blue); int x=color.getRGB(); back.setRGB(j,i,x); } } return back; } catch (Exception e) { e.printStackTrace(); return null; } }Image reading, and storage functions
//Read the image public static BufferedImage file2img(String imgpath) { try { BufferedImage bufferedImage=ImageIO.read(new File(imgpath)); return bufferedImage; } catch (Exception e) { e.printStackTrace(); return null; } } //Save the image, the extent is in the format, "jpg", "png", etc. public static void img2file(BufferedImage img,String extent,String newfile) { try { ImageIO.write(img, extent, new File(newfile)); } catch (Exception e) { e.printStackTrace(); } }share:
The following is a calculation formula for brightness and contrast
(RGB represents the value of the color component of the original image, the value that is not processed by nRGB table, mBrightness represents the adjusted brightness value, mContrast represents the adjusted contrast value, and avg represents the average value of the entire image pixel)
Brightness:nRGB=RGB+mBrightness
Contrast: nRGB=(RGB-avg)*(1-percent%)+avg percent% value range is (-1~1) 0 is the original value. The contrast formula is also very good to prove. Expand it.
nRGB=RGB-RGB*percent%-avg+avg*percent%+avg
nRGB=RGB-RGB*percent%+avg*percent%
For the entire image matrix, the brightness must be ensured that the algebraic sum of the entire matrix is unchanged.
And avg=(RGB1+RGB2+...RGBn)/n (1)
(nRGB1+nRGB2+...nRGBn)=(RGB1+RGB2+...RGBn)+n*avg*percent%-(RGB1+RGB2+...RGBn)*percent% (2)
Just substitute formula (1) into formula (2)
Summarize
The above is all the content of this article about the color level adjustment and brightness adjustment code examples of Java picture, I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out.