java 多线程-图片去水印

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

01传说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值