java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整-創(chuàng)新互聯(lián)

這篇文章主要講解了“java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整”吧!

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供化隆網(wǎng)站建設(shè)、化隆做網(wǎng)站、化隆網(wǎng)站設(shè)計、化隆網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、化隆企業(yè)網(wǎng)站模板建站服務(wù),十余年化隆做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

java實(shí)現(xiàn)色階調(diào)整,即調(diào)整圖片rgb分量,進(jìn)而也可以調(diào)節(jié)圖片亮度。

測試代碼

public static void main(String[] args) {
	//文件與BufferedImage間的轉(zhuǎn)換
	BufferedImage bi=file2img("test.jpg");
	//讀取圖片
	BufferedImage bii=img_color_gradation(bi,100,0,0);
	img2file(bii,"jpg","test1.jpg");
	//生成圖片
}

色階調(diào)整代碼

//圖片色階調(diào)整,調(diào)整rgb的分量
  public static BufferedImage img_color_gradation(BufferedImage imgsrc, int r, int g, int b) {
    try {
      //創(chuàng)建一個不帶透明度的圖片
      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;
    }
  }

圖片讀取,和存儲函數(shù)

//讀取圖片
  public static BufferedImage file2img(String imgpath) {
    try {
      BufferedImage bufferedImage=ImageIO.read(new File(imgpath));
      return bufferedImage;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  //保存圖片,extent為格式,"jpg"、"png"等
  public static void img2file(BufferedImage img,String extent,String newfile) {
    try {
      ImageIO.write(img, extent, new File(newfile));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

分享:

下面先給出亮度和對比度的計算公式

(RGB表示原圖的色彩分量的值,nRGB表不處理后的值,mBrightness表示調(diào)整的亮度值,mContrast表示調(diào)整的對比度值,avg表示整個圖像像素的平均值)
亮度:nRGB=RGB+mBrightness
對比度:nRGB=(RGB-avg)*(1-percent%)+avg   percent%取值范圍為(-1~1) 0為原始值
對比度公式也很好證明,將其展開
nRGB=RGB-RGB*percent%-avg+avg*percent%+avg
nRGB=RGB-RGB*percent%+avg*percent%
對于整個圖像矩陣來說,要保證亮度不變,即整個矩陣的代數(shù)和不變。
而avg=(RGB1+RGB2+....RGBn)/n    (1)
(nRGB1+nRGB2+.....nRGBn)=(RGB1+RGB2+...RGBn)+n*avg*percent%-(RGB1+RGB2+...RGBn)*percent%  (2)
將(1)式代入(2)式即可

感謝各位的閱讀,以上就是“java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

新聞標(biāo)題:java怎么實(shí)現(xiàn)圖片色階調(diào)整和亮度調(diào)整-創(chuàng)新互聯(lián)
標(biāo)題來源:http://muchs.cn/article44/dsphee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)微信公眾號、網(wǎng)站制作、響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化