java圖片色階調(diào)整和亮度調(diào)整代碼示例
對(duì)Java圖片處理的內(nèi)容涉獵不深,言辭簡(jiǎn)陋望請(qǐng)見諒。
java實(shí)現(xiàn)色階調(diào)整,即調(diào)整圖片rgb分量,進(jìn)而也可以調(diào)節(jié)圖片亮度。
測(cè)試代碼
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)建一個(gè)不帶透明度的圖片 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; } }
圖片讀取,和存儲(chǔ)函數(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(); } }
分享:
下面先給出亮度和對(duì)比度的計(jì)算公式
(RGB表示原圖的色彩分量的值,nRGB表不處理后的值,mBrightness表示調(diào)整的亮度值,mContrast表示調(diào)整的對(duì)比度值,avg表示整個(gè)圖像像素的平均值)
亮度:nRGB=RGB+mBrightness
對(duì)比度:nRGB=(RGB-avg)*(1-percent%)+avg percent%取值范圍為(-1~1) 0為原始值
對(duì)比度公式也很好證明,將其展開
nRGB=RGB-RGB*percent%-avg+avg*percent%+avg
nRGB=RGB-RGB*percent%+avg*percent%
對(duì)于整個(gè)圖像矩陣來(lái)說,要保證亮度不變,即整個(gè)矩陣的代數(shù)和不變。
而avg=(RGB1+RGB2+....RGBn)/n (1)
(nRGB1+nRGB2+.....nRGBn)=(RGB1+RGB2+...RGBn)+n*avg*percent%-(RGB1+RGB2+...RGBn)*percent% (2)
將(1)式代入(2)式即可
總結(jié)
以上就是本文關(guān)于java圖片色階調(diào)整和亮度調(diào)整代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。
相關(guān)文章
Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程
這篇文章主要介紹了Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程,包括基于TCP/IP和UDP協(xié)議的簡(jiǎn)單實(shí)例程序講解,需要的朋友可以參考下2016-01-01使用@Validated注解進(jìn)行校驗(yàn)卻沒有效果的解決
這篇文章主要介紹了使用@Validated注解進(jìn)行校驗(yàn)卻沒有效果的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04java 刪除文件夾中的所有內(nèi)容而不刪除文件夾本身的實(shí)例
下面小編就為大家?guī)?lái)一篇java 刪除文件夾中的所有內(nèi)容而不刪除文件夾本身的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-12-12spring?retry實(shí)現(xiàn)方法請(qǐng)求重試的使用步驟
這篇文章主要介紹了spring?retry實(shí)現(xiàn)方法請(qǐng)求重試及使用步驟,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07java中@JsonValue和@JsonCreator使用
本文主要介紹了java中@JsonValue和@JsonCreator使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06java操作mysql實(shí)現(xiàn)增刪改查的方法
這篇文章主要介紹了java操作mysql實(shí)現(xiàn)增刪改查的方法,結(jié)合實(shí)例形式分析了java操作mysql數(shù)據(jù)庫(kù)進(jìn)行增刪改查的具體實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類
這篇文章主要介紹了使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Spring cloud Gateway簡(jiǎn)介及相關(guān)配置方法
這篇文章主要介紹了Spring cloud Gateway簡(jiǎn)介及相關(guān)配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04