java將圖片至暗的實(shí)現(xiàn)方法
之前也寫過一個(gè)代碼給一張圖片然后把圖片變暗,今天我們換一種思路,或者是是另外的一種方式將圖片至暗,當(dāng)然方法也是很簡單的,但是對(duì)于菜鳥的我在這個(gè)地方停留了一天半的時(shí)間,將圖片至暗
現(xiàn)在我們要將這樣的一張圖片

變成為

雖然說變暗之后確實(shí)沒有之間亮的好看,但是不管了,反正那么漂亮的美女和我的關(guān)系我不太大,如果說硬是有關(guān)系的話,那應(yīng)該是在夢(mèng)中了,好了我們直接上代碼
package com.epoint.wdg.test;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImgTest {
public static void main(String[] args) throws IOException {
File file=new File("C://Users/wdg/Desktop/people.png");
//showParamterofImg(file);
File file2=changeImgtoGray(file);
grayPicToBW(file2);
}
public static void getRGB(File file) throws IOException{
int []rgb =new int[3];
BufferedImage img=ImageIO.read(file);
int pixel=img.getRGB(2, 3);
// 下面三行代碼將一個(gè)數(shù)字轉(zhuǎn)換為RGB數(shù)字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println(rgb[0]+"-"+rgb[1]+"-"+rgb[2]);
}
//把圖片變灰色
public static File changeImgtoGray(File file) throws IOException{
float []rgb =new float[3];
BufferedImage img=ImageIO.read(file);
//現(xiàn)在我需要獲取到?jīng)]一點(diǎn)的rgb
int y=img.getHeight();
int x=img.getWidth();
BufferedImage grayImage = new BufferedImage(x, y, BufferedImage.TYPE_BYTE_GRAY);
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
int pixel=img.getRGB(i, j);
// grayImage.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
int gray=(int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11);
Color color=new Color(gray,gray,gray);
img.setRGB(i, j, color.getRGB());
}
}
File newFile = new File("C://Users/wdg/Desktop/"+"/method5.jpg");
ImageIO.write(img, "jpg", newFile);
// grayPicToBW(newFile);
return newFile;
}
其中最為重要的是這一部分:
int y=img.getHeight();
int x=img.getWidth();
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
int pixel=img.getRGB(i, j);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
int gray=(int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11);
Color color=new Color(gray,gray,gray);
img.setRGB(i, j, color.getRGB());
}
}
File newFile = new File("C://Users/wdg/Desktop/"+"/method5.jpg");
ImageIO.write(img, "jpg", newFile);
這一部分是獲取到到圖片的每一點(diǎn)的像素或者說ARGB:
int pixel=img.getRGB(i, j);
然后進(jìn)一步的獲取到RGB,然后我們獲取到這點(diǎn)像素的灰度值,
int gray=(int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11);
并且創(chuàng)建一個(gè)顏色:
lor color=new Color(gray,gray,gray); img.setRGB(i, j, color.getRGB());
這樣我們將圖片打印出來就是我們第二張圖片那樣了
以上這篇java將圖片至暗的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中@EnableAutoConfiguration和@Configuration的區(qū)別
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration和@Configuration的區(qū)別,@SpringBootApplication相當(dāng)于@EnableAutoConfiguration,@ComponentScan,@Configuration三者的集合,需要的朋友可以參考下2023-08-08
JavaFX桌面應(yīng)用未響應(yīng)問題解決方案
這篇文章主要介紹了JavaFX桌面應(yīng)用未響應(yīng)問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java 創(chuàng)建線程的3種方法及各自的優(yōu)點(diǎn)
這篇文章主要介紹了Java 創(chuàng)建線程的3種方法及各自的優(yōu)點(diǎn),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
java生成申請(qǐng)單序列號(hào)的實(shí)現(xiàn)方法
申請(qǐng)單序列號(hào)一般要求根據(jù)一定的規(guī)則生成后幾位連續(xù)的字符串,下面是我項(xiàng)目中使用的生成序列號(hào)的代碼,其中用到了鎖機(jī)制,有需要的朋友可以參考一下2014-01-01

