java代码多线程批量处理文件内的图片去水印
1、上效果
2、上代码
package com.aaaa.yaojian.util;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/***
* Desc:
* @author lisong
*
*/
@Slf4j
public class ImageUtil {
// 开启10个线程
private static final int NUM_THREADS = 10;
public static void main(String[] args) {
File folder = new File("D:\\meituanpic\\meituanpic1\\xianfu");
File[] files = folder.listFiles();
if (files == null) {
return; // 文件夹为空或者读取失败
}
int numFiles = files.length;
int batchSize = numFiles / NUM_THREADS;
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++) {
final int start = i * batchSize;
final int end = (i == NUM_THREADS - 1) ? numFiles : (i + 1) * batchSize;
executor.submit(new FileProcessorTask(files, start, end));
}
executor.shutdown();
}
private static class FileProcessorTask implements Runnable {
private File[] files;
private int start;
private int end;
public FileProcessorTask(File[] files, int start, int end) {
this.files = files;
this.start = start;
this.end = end;
}
@Override
public void run() {
java.util.List<File> list1 = new ArrayList<>();
for (int i = start; i < end; i++) {
// File file = files[i];
getmain(files[i].getPath(),files[i].getName());
// 分批处理文件的逻辑
// list1.add(files[i]);
// if(list1.size() == 100){
// for(File file1 : list1){
// getmain(file1.getPath(),file1.getName());
// }
// list1.clear();
// }
}
// if(list1.size()>1){
// for(File file1 : list1){
// getmain(file1.getPath(),file1.getName());
// }
// list1.clear();
// }
}
}
public static void yidong( File sourceFile,String destinationDirPath){
File destinationDirectory = new File(destinationDirPath);
File destinationFile = new File(destinationDirectory, sourceFile.getName());
sourceFile.renameTo(destinationFile);
}
public static void getmain(String imgUrl,String imgName){
try{
File file = new File(imgUrl);
BufferedImage bi = ImageIO.read(file);
if(bi!=null){
int pointNum = 5;
double[][] xy = new double[2][5 * pointNum];
double[][] rgb = new double[3][5 * pointNum];
int widthBorder = 5;
double w = bi.getWidth()*0.35;
int width = (int)w;
// double widthStep = (double) (width - widthBorder * 2) / (pointNum - 1);
double widthStep = (double) (width - widthBorder * 2) / (pointNum - 1);
int heightBorder = 5;
double h = bi.getHeight()*0.20;
int height = (int)h;
// double heightStep = (double) (height - heightBorder * 2) / (pointNum - 1);
double heightStep = (double) (height - heightBorder * 2) / (pointNum - 1);
for (int i = 0;