本篇文章為大家展示了利用java怎么實(shí)現(xiàn)一個(gè)將圖片去色的功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
現(xiàn)在我們要將這樣的一張圖片
變成為
代碼
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> 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; }