背景:
如下圖所示:圖片電子章那一塊的區域的背景色有點深,想將其改為純白色,與整個圖片的背景色調一致,
仔細看的話,可以看到紅框中,左邊一半的顏色比右邊一半的稍深一些
效果:
可以看到,原來電子章的背景色有點深,現在是純白色了,而且不影響電子章的顯示效果
代碼:
public class Test { public static void main(String[] args) { getImageGRB("E:\\222.png"); } public static int[][] getImageGRB(String filePath) { File file = new File(filePath); int[][] result = null; if (!file.exists()) { return result; } try { BufferedImage bufImg = ImageIO.read(file); int height = bufImg.getHeight(); int width = bufImg.getWidth(); result = new int[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { result[i][j] = bufImg.getRGB(i, j) & 0xFFFFFF; int pixel = bufImg.getRGB(i, j); int rgbR = (pixel & 0xff0000) >> 16; int rgbG = (pixel & 0xff00) >> 8; int rgbB = (pixel & 0xff); System.out.println("rgbR:" + rgbR + ",rgbG:" + rgbG+ ",rgbB" + rgbB);
//240這個值,比電子章的背景色的RGB值稍微小一點。可以上下浮動多試試這個值,調一調哪個閾值最好 if(rgbR > 240 && rgbG > 240 && rgbB > 240){ //將圖片中大於240的色值,設為白色 bufImg.setRGB(i, j, new Color(255,255,255).getRGB()); } } } File outputfile = new File("E:\\222.png"); ImageIO.write(bufImg, "png", outputfile); } catch (IOException e) { e.printStackTrace(); } return result; } }
原理是:
一張圖片是由一個個RGB的位圖(一個個點)組成,當這些點密度達到一定程度,我們看着就是一副完整的圖。
我們遍歷這些點的RGB顏色,將獲取需要更改的顏色的RGB的值,將其改為想要的值
PS:
取色器:可以通過Windows自帶的畫圖軟件,獲取圖片RGB的值