Python 官方文档:入门教程 => 点击学习
本文实例为大家分享了java实现图片反色处理的具体代码,供大家参考,具体内容如下 效果对比 原图 反色处理 原图 反色处理 核心代码实现 import javax.ima
本文实例为大家分享了java实现图片反色处理的具体代码,供大家参考,具体内容如下
原图
反色处理
原图
反色处理
核心代码实现
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageColor {
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();
}
}
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十六位制的蓝色
运行主方法即可。
--结束END--
本文标题: java实现图片反色处理示例
本文链接: https://www.lsjlt.com/news/157538.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0