java实现图片去掉水印
java实现图⽚去掉⽔印
代码
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.ssl.HttpsURLConnection;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.HttpURLConnection;
import java.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class AAAA {
private static List fileList = new ArrayList();
public static void main(String[] args) {
//图⽚所在的根⽬录 , 图⽚去除⽔印后的存储⽬录
convertAllImages(“D:\江西\江西.jpg”, “D:\IcPhoto1\江西.jpg”); //⽀持批量去除图⽚⽔印换届策划书
}
private static void convertAllImages(String dir, String saveDir) {
File dirFile = new File(dir);
File saveDirFile = new File(saveDir);
dir = AbsolutePath();
saveDir = AbsolutePath();
loadImages(new File(dir));
for (File file : fileList) {
String filePath = AbsolutePath();
String dstPath = saveDir + filePath.substring(filePath.indexOf(dir) + dir.length(), filePath.length()); System.out.println("converting: " + filePath);
AbsolutePath(), 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 (dsWith(“png”) || dsWith(“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 = Name();
if (dsWith(“png”) || dsWith(“jpg”)) { return true;
}
return false;
}
});
for (File img : files) {
AbsolutePath(), AbsolutePath()); }
}
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 (im().startsWith(“https”)) {
http = new URL(file);
HttpsURLConnection conn = (HttpsURLConnection) http.openConnection();
conn.setRequestMethod(“GET”);
} else if (im().startsWith(“http”)) {
http = new URL(file);
HttpURLConnection conn = (HttpURLConnection) http.openConnection();
铜陵旅游
conn.setRequestMethod(“GET”);
} else {
http = new File(file).toURI().toURL();
}
BufferedImage bi = ad(http.openStream());
if(bi == null){
return;
}
/
/Color wColor = new Color(255, 255, 255);//⽩⾊
Color wColor = new Color(238, 243, 249);//浅灰⾊
for (int i = 0; i < bi.getWidth(); i++) {
for (int j = 0; j < bi.getHeight(); j++) {
//System.out.RGB(i, j));
int color = bi.getRGB(i, j);
Color oriColor = new Color(color);
int red = Red();
int greed = Green();
int blue = Blue();
//粉⾊
if (greed < 190 || blue < 190) {
} else {
//去掉粉⾊⽔印(粉⾊替换为⽩⾊)
// if (red == 255 && greed > 180 && blue > 180) {
// bi.setRGB(i, j, RGB());
// }
//去掉灰⾊⽔印(灰⾊替换为⽩⾊)
// if (red == 229 && greed == 229 && blue == 229) {数学与应用数学就业方向
// bi.setRGB(i, j, RGB());
农贸市场管理制度// }
//去掉浅灰⾊⽔印(灰⾊替换为⽩⾊或替换为浅灰⾊)
if (red >170 && greed > 170 && blue > 170) {
bi.setRGB(i, j, RGB());
}
}
}
}
String type = file.substring(file.lastIndexOf(".") + 1, file.length()); Iterator it = ImageWritersByFormatName(type); ImageWriter writer = it.next();
File f = new File(dstFile);
ImageOutputStream ios = ateImageOutputStream(f); writer.setOutput(ios);
writer.write(bi);
bi.flush();
ios.flush();
ios.close();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。