java实现图片反色处理示例
作者:洛阳泰山 时间:2022-07-09 20:53:34
本文实例为大家分享了java实现图片反色处理的具体代码,供大家参考,具体内容如下
效果对比
原图
反色处理
原图
反色处理
核心代码实现
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageColor {
/**
* @Description: 反色
* @param imgPath 图片路径
* @param fileUrl 输出图片路径
* @throws
*/
public static void inverse(String imgPath, String fileUrl){
try {
FileInputStream fileInputStream = new FileInputStream(imgPath);
BufferedImage image = ImageIO.read(fileInputStream);
//生成字符图片
int w = image.getWidth();
int h = image.getHeight();
BufferedImage imageBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);;
// 绘制字符
for (int y = 0; y < h; y++) {
for (int x = 0; x< w; x++) {
int rgb = image.getRGB(x, y);
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0x00ff00) >> 8;
int B = rgb & 0x0000ff;
int newPixel=colorToRGB(255-R,255-G,255-B);
imageBuffer.setRGB(x,y,newPixel);
}
}
ImageIO.write(imageBuffer, "png", new File(fileUrl)); //输出图片
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Description: 颜色转rgb值
* @throws
*/
public static int colorToRGB(int red,int green,int blue){
int newPixel=0;
newPixel=newPixel << 8;
newPixel+=red;
newPixel=newPixel << 8;
newPixel+=green;
newPixel=newPixel << 8;
newPixel+=blue;
return newPixel;
}
public static void main(String[] args) throws IOException {
inverse("C:\\Users\\liuya\\Desktop\\laoying.png","C:\\Users\\liuya\\Desktop\\logo_0.png");
}
}
补充知识
三基色是光的红,绿,蓝
0xff0000 为RGB十六位制的红色
0x00ff00 为RGB十六位制的绿色
0x0000ff 为RGB十六位制的蓝色
运行主方法即可。
来源:https://blog.csdn.net/weixin_40986713/article/details/121332852
标签:java,图片反色
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter 实现虎牙/斗鱼 弹幕功能
2023-07-11 04:11:11
![](https://img.aspxhome.com/file/2023/8/138068_0s.gif)
Spring IOC:CreateBean环节中的流程转换
2022-06-10 12:28:31
![](https://img.aspxhome.com/file/2023/3/64903_0s.jpg)
c#的params参数使用示例
2021-10-07 04:53:39
浅谈Java数值类型的转换与强制转换
2022-07-01 15:24:00
![](https://img.aspxhome.com/file/2023/9/89949_0s.png)
android照相、相册获取图片剪裁报错的解决方法
2021-11-21 20:18:14
详解Java豆瓣电影爬虫——小爬虫成长记(附源码)
2023-10-29 17:45:17
![](https://img.aspxhome.com/file/2023/7/84287_0s.png)
Kotlin 高阶函数与Lambda表达式示例详解
2021-06-18 08:58:15
![](https://img.aspxhome.com/file/2023/2/138792_0s.png)
浅谈java中==以及equals方法的用法
2021-09-29 15:21:45
Java8方法引用及构造方法引用原理实例解析
2022-07-24 08:21:22
Java线程的生命周期命名与获取代码实现
2022-05-21 05:19:49
![](https://img.aspxhome.com/file/2023/5/64145_0s.jpg)
Android 管理Activity中的fragments
2022-06-30 16:41:37
![](https://img.aspxhome.com/file/2023/7/137197_0s.png)
Android定时器和倒计时实现淘宝秒杀功能
2023-01-18 02:17:04
![](https://img.aspxhome.com/file/2023/3/137153_0s.png)
C# DateTime.ToString根据不同语言生成相应的时间格式
2022-04-16 12:34:51
![](https://img.aspxhome.com/file/2023/4/113774_0s.png)
Java HashMap三种循环遍历方式及其性能对比实例分析
2022-03-22 18:36:45
Android自定义顶部标题栏
2023-02-21 19:03:47
![](https://img.aspxhome.com/file/2023/7/109167_0s.jpg)
Kotlin编程循环控制示例详解
2022-06-02 11:53:54
![](https://img.aspxhome.com/file/2023/3/110753_0s.webp)
java中的Arrays这个工具类你真的会用吗(一文秒懂)
2021-11-28 06:25:11
Android互联网访问图片并在客户端显示的方法
2021-12-26 21:25:10
![](https://img.aspxhome.com/file/2023/1/138341_0s.jpg)
C语言关于时间复杂度详解
2022-08-14 02:33:04
![](https://img.aspxhome.com/file/2023/5/122575_0s.jpg)
Netty分布式高性能工具类异线程下回收对象解析
2023-07-09 21:02:43
![](https://img.aspxhome.com/file/2023/2/99882_0s.png)