java實(shí)現(xiàn)圖片反色處理示例
本文實(shí)例為大家分享了java實(shí)現(xiàn)圖片反色處理的具體代碼,供大家參考,具體內(nèi)容如下
效果對(duì)比
原圖

反色處理

原圖

反色處理

核心代碼實(shí)現(xiàn)
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: 顏色轉(zhuǎn)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");
}
}
補(bǔ)充知識(shí)
三基色是光的紅,綠,藍(lán)
0xff0000 為RGB十六位制的紅色
0x00ff00 為RGB十六位制的綠色
0x0000ff 為RGB十六位制的藍(lán)色
運(yùn)行主方法即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 中 synchronized的用法詳解(四種用法)
Java語(yǔ)言的關(guān)鍵字,當(dāng)它用來修飾一個(gè)方法或者一個(gè)代碼塊的時(shí)候,能夠保證在同一時(shí)刻最多只有一個(gè)線程執(zhí)行該段代碼。本文給大家介紹java中 synchronized的用法,對(duì)本文感興趣的朋友一起看看吧2015-11-11
Java SpringBoot實(shí)現(xiàn)AOP
AOP包括連接點(diǎn)(JoinPoint)、切入點(diǎn)(Pointcut)、增強(qiáng)(Advisor)、切面(Aspect)、AOP代理(AOP Proxy),具體的方法和類型下面文章會(huì)舉例說明,感興趣的小伙伴和小編一起閱讀全文吧2021-09-09
Java 線程池ExecutorService詳解及實(shí)例代碼
這篇文章主要介紹了Java 線程池ExecutorService詳解及實(shí)例代碼的相關(guān)資料,線程池減少在創(chuàng)建和銷毀線程上所花的時(shí)間以及系統(tǒng)資源的開銷.如果不使用線程池,有可能造成系統(tǒng)創(chuàng)建大量線程而導(dǎo)致消耗系統(tǒng)內(nèi)存以及”過度切換“2016-11-11

