温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用java怎么实现一个将图片去色的功能

发布时间:2020-11-21 15:57:45 来源:亿速云 阅读:169 作者:Leah 栏目:编程语言

本篇文章为大家展示了利用java怎么实现一个将图片去色的功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

现在我们要将这样的一张图片

变成为

利用java怎么实现一个将图片去色的功能

代码

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); 
    // 下面三行代码将一个数字转换为RGB数字  
    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); 
    //现在我需要获取到没一点的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);  

这一部分是获取到到图片的每一点的像素或者说ARGB:

int pixel=img.getRGB(i, j); 

然后进一步的获取到RGB,然后我们获取到这点像素的灰度值,

int gray=(int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11); 

并且创建一个颜色:

lor color=new Color(gray,gray,gray); 

img.setRGB(i, j, color.getRGB()); 

上述内容就是利用java怎么实现一个将图片去色的功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI