1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| public class ImageConverter { private static List<File> fileList = new ArrayList<File>();
public static void main(String[] args) { System.out.println("输入需要去水印的图片所在的根目录回车(支持递归子目录):"); Scanner input = new Scanner(System.in); String dir ="C:\\Users\\zengwei\\Desktop\\112"; String saveDir = "C:\\Users\\zengwei\\Desktop"; System.out.println("输入y开始"); String comfrm = input.nextLine().trim(); if (comfrm.equals("y")) { convertAllImages(dir, saveDir); } else { System.out.println("您输入的不是y程序,程序退出"); }
/*String dir = "E:\\data\\Desktop"; String saveDir = "E:\\data\\Desktop-convert";*/
//replaceFolderImages(dir); /*String srcFile = "E:\\data\\image\\img38.png"; String dstFile = "E:\\data\\image\\img38.png"; long begin = System.currentTimeMillis(); repleaceColor(srcFile, dstFile); long time = System.currentTimeMillis() - begin; System.out.println(time);*/ }
private static void convertAllImages(String dir, String saveDir) { File dirFile = new File(dir); File saveDirFile = new File(saveDir); dir = dirFile.getAbsolutePath(); saveDir = saveDirFile.getAbsolutePath(); loadImages(new File(dir)); for (File file : fileList) { String filePath = file.getAbsolutePath();
String dstPath = saveDir + filePath.substring(filePath.indexOf(dir) + dir.length(), filePath.length()); System.out.println("converting: " + filePath); replaceColor(file.getAbsolutePath(), dstPath); } }
public static void loadImages(File f) { if (f != null) { if (f.isDirectory()) { File[] fileArray = f.listFiles(); if (fileArray != null) { for (int i = 0; i < fileArray.length; i++) { //递归调用 loadImages(fileArray[i]); } } } else { String name = f.getName(); if (name.endsWith("png") || name.endsWith("jpg")) { fileList.add(f); } } } }
private static void replaceFolderImages(String dir) { File dirFile = new File(dir); File[] files = dirFile.listFiles(new FileFilter() { public boolean accept(File file) { String name = file.getName(); if (name.endsWith("png") || name.endsWith("jpg")) { return true; } return false; } }); for (File img : files) { replaceColor(img.getAbsolutePath(), img.getAbsolutePath()); } }
private static void replaceColor(String srcFile, String dstFile) { try { Color color = new Color(255, 195, 195); replaceImageColor(srcFile, dstFile, color, Color.WHITE); } catch (IOException e) { e.printStackTrace(); } }
public static void replaceImageColor(String file, String dstFile, Color srcColor, Color targetColor) throws IOException { URL http; if (file.trim().startsWith("https")) { http = new URL(file); HttpsURLConnection conn = (HttpsURLConnection) http.openConnection(); conn.setRequestMethod("GET"); } else if (file.trim().startsWith("http")) { http = new URL(file); HttpURLConnection conn = (HttpURLConnection) http.openConnection(); conn.setRequestMethod("GET"); } else { http = new File(file).toURI().toURL(); } BufferedImage bi = ImageIO.read(http.openStream()); if(bi == null){ return; }
Color wColor = new Color(255, 255, 255); for (int i = 0; i < bi.getWidth(); i++) { for (int j = 0; j < bi.getHeight(); j++) { //System.out.println(bi.getRGB(i, j)); int color = bi.getRGB(i, j); Color oriColor = new Color(color); int red = oriColor.getRed(); int greed = oriColor.getGreen(); int blue = oriColor.getBlue(); //粉色 if (greed < 190 || blue < 190) {
} else { if (red == 255 && greed > 180 && blue > 180) { bi.setRGB(i, j, wColor.getRGB()); } } } } String type = file.substring(file.lastIndexOf(".") + 1, file.length()); Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(type); ImageWriter writer = it.next(); File f = new File(dstFile); f.getParentFile().mkdirs(); ImageOutputStream ios = ImageIO.createImageOutputStream(f); writer.setOutput(ios); writer.write(bi); bi.flush(); ios.flush(); ios.close(); } }
|