Pārlūkot izejas kodu

feat: 去除水印测试

chenjianhua 4 mēneši atpakaļ
vecāks
revīzija
57b84cf20d

+ 30 - 10
service-base/pom.xml

@@ -203,16 +203,16 @@
             <artifactId>jjwt</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.pdfbox</groupId>
-            <artifactId>pdfbox</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.slf4j</groupId>
-                    <artifactId>slf4j-simple</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.apache.pdfbox</groupId>-->
+<!--            <artifactId>pdfbox</artifactId>-->
+<!--            <exclusions>-->
+<!--                <exclusion>-->
+<!--                    <groupId>org.slf4j</groupId>-->
+<!--                    <artifactId>slf4j-simple</artifactId>-->
+<!--                </exclusion>-->
+<!--            </exclusions>-->
+<!--        </dependency>-->
 
         <dependency>
             <groupId>technology.tabula</groupId>
@@ -279,6 +279,26 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itext-asian</artifactId>
+            <version>5.2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.cssbox</groupId>
+            <artifactId>pdf2dom</artifactId>
+            <version>2.0.3</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.pdfbox</groupId>
+            <artifactId>pdfbox-tools</artifactId>
+            <version>2.0.24</version>
+        </dependency>
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>5.5.10</version>
+        </dependency>
     </dependencies>
 
 <!--    <build>-->

+ 303 - 86
service-deploy/src/test/java/com/simuwang/ApplicationTest.java

@@ -1,89 +1,306 @@
-//package com.simuwang;
+package com.simuwang;
+
+import cn.hutool.core.collection.ListUtil;
+import cn.hutool.core.date.DateUtil;
+import com.itextpdf.text.Image;
+import com.itextpdf.text.pdf.PdfWriter;
+import com.simuwang.base.common.conts.DateConst;
+import com.simuwang.base.pojo.dto.MailboxInfoDTO;
+import com.simuwang.daq.service.EmailParseApiService;
+import com.simuwang.daq.service.EmailParseService;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.rendering.ImageType;
+import org.apache.pdfbox.rendering.PDFRenderer;
+import org.jasypt.util.text.BasicTextEncryptor;
+import org.apache.pdfbox.Loader;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import com.itextpdf.text.*;
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.*;
+import java.util.List;
+
+
+@SpringBootTest(classes = Application.class)
+public class ApplicationTest {
+
+    @Autowired
+    private EmailParseService emailParseService;
+
+    @Autowired
+    private EmailParseApiService emailParseApiService;
+
+    @Test
+    public void test() {
+        MailboxInfoDTO emailInfoDTO = this.buildMailbox("", "");
 //
-//import cn.hutool.core.collection.ListUtil;
-//import cn.hutool.core.date.DateUtil;
-//import com.simuwang.base.common.conts.DateConst;
-//import com.simuwang.base.common.util.DateUtils;
-//import com.simuwang.base.pojo.dto.MailboxInfoDTO;
-//import com.simuwang.daq.service.EmailParseApiService;
-//import com.simuwang.daq.service.EmailParseService;
-//import org.jasypt.util.text.BasicTextEncryptor;
-//import org.junit.jupiter.api.Test;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import org.springframework.boot.test.context.SpringBootTest;
-//
-//import java.util.ArrayList;
-//import java.util.Date;
-//
-//@SpringBootTest(classes = Application.class)
-//public class ApplicationTest {
-//
-//    @Autowired
-//    private EmailParseService emailParseService;
-//
-//    @Autowired
-//    private EmailParseApiService emailParseApiService;
-//
-//    @Test
-//    public void test() {
-//        MailboxInfoDTO emailInfoDTO = this.buildMailbox("", "");
-////
-////        emailInfoDTO.setAccount("jjpj_test");
-////        emailInfoDTO.setPassword("shzq#919");
-////        emailInfoDTO.setHost("mail.shzq.com");
-////        emailInfoDTO.setPort("993");
-////        emailInfoDTO.setProtocol("imap");
-//
-//        Date startDate = DateUtil.parse("2024-10-10 15:00:00", DateConst.YYYY_MM_DD_HH_MM_SS);
-//        Date endDate = DateUtil.parse("2024-10-10 16:40:00", DateConst.YYYY_MM_DD_HH_MM_SS);
-//        try {
-//            emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
-//        } catch (Exception e) {
-//            throw new RuntimeException(e);
-//        }
-//    }
-//
-//    @Test
-//    public void reportTest() {
-//        MailboxInfoDTO emailInfoDTO = this.buildMailbox("x", "x");
-//        Date startDate = DateUtil.parse("2024-10-15 15:10:30", DateConst.YYYY_MM_DD_HH_MM_SS);
-//        Date endDate = DateUtil.parse("2024-10-15 17:50:30", DateConst.YYYY_MM_DD_HH_MM_SS);
-//        try {
-//            emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
-//        } catch (Exception e) {
-//            throw new RuntimeException(e);
-//        }
-//    }
-//
-//    @Test
-//    public void testReparseEmail() {
-//        emailParseApiService.reparseEmail(593);
-//    }
-//
-//    @Test
-//    public void testReparseFile() {
-//        emailParseApiService.reparseFile(ListUtil.toList(40, 43));
-//    }
-//
-//    @Test
-//    public void testDateFormat() {
-//        String input = "Smppw@2024";
-//        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
-//        textEncryptor.setPassword("qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm12"); // 用您自己的密钥替换"yourSecretKey"
-//        String encryptedString = textEncryptor.encrypt(input);
-//        String decrypt = textEncryptor.decrypt(encryptedString);
-//        System.out.println("加密后的字符串为: " + encryptedString);
-//        System.out.println("解密后的字符串为: " + decrypt);
-//    }
-//
-//    private MailboxInfoDTO buildMailbox(String account, String pwd) {
-//        MailboxInfoDTO emailInfoDTO = new MailboxInfoDTO();
-//        emailInfoDTO.setUserId(1);
-//        emailInfoDTO.setAccount(account);
-//        emailInfoDTO.setPassword(pwd);
-//        emailInfoDTO.setHost("imap.exmail.qq.com");
+//        emailInfoDTO.setAccount("jjpj_test");
+//        emailInfoDTO.setPassword("shzq#919");
+//        emailInfoDTO.setHost("mail.shzq.com");
 //        emailInfoDTO.setPort("993");
 //        emailInfoDTO.setProtocol("imap");
-//        return emailInfoDTO;
-//    }
-//}
+
+        Date startDate = DateUtil.parse("2024-10-10 15:00:00", DateConst.YYYY_MM_DD_HH_MM_SS);
+        Date endDate = DateUtil.parse("2024-10-10 16:40:00", DateConst.YYYY_MM_DD_HH_MM_SS);
+        try {
+            emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    public void reportTest() {
+        MailboxInfoDTO emailInfoDTO = this.buildMailbox("x", "x");
+        Date startDate = DateUtil.parse("2024-10-15 15:10:30", DateConst.YYYY_MM_DD_HH_MM_SS);
+        Date endDate = DateUtil.parse("2024-10-15 17:50:30", DateConst.YYYY_MM_DD_HH_MM_SS);
+        try {
+            emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    public void testReparseEmail() {
+        emailParseApiService.reparseEmail(593);
+    }
+
+    @Test
+    public void testReparseFile() {
+        emailParseApiService.reparseFile(ListUtil.toList(40, 43));
+    }
+
+    @Test
+    public void testDateFormat() {
+        String input = "Smppw@2024";
+        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
+        textEncryptor.setPassword("qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm12"); // 用您自己的密钥替换"yourSecretKey"
+        String encryptedString = textEncryptor.encrypt(input);
+        String decrypt = textEncryptor.decrypt(encryptedString);
+        System.out.println("加密后的字符串为: " + encryptedString);
+        System.out.println("解密后的字符串为: " + decrypt);
+    }
+
+    private MailboxInfoDTO buildMailbox(String account, String pwd) {
+        MailboxInfoDTO emailInfoDTO = new MailboxInfoDTO();
+        emailInfoDTO.setUserId(1);
+        emailInfoDTO.setAccount(account);
+        emailInfoDTO.setPassword(pwd);
+        emailInfoDTO.setHost("imap.exmail.qq.com");
+        emailInfoDTO.setPort("993");
+        emailInfoDTO.setProtocol("imap");
+        return emailInfoDTO;
+    }
+
+    @Test
+    public void testPdfRemoveWatermark() {
+        pdfRemoveWatermark("D:/pdf/东华期货奔牛1号7.29-8.2基金净值表.pdf");
+    }
+
+    public void pdfRemoveWatermark(String filePath) {
+        try {
+            //PDF转图片
+            pdfToImage(filePath);
+            List<String> fileNames = getFileNames("D:/pdf/pdfToImage");
+            List<File> fileList = new ArrayList<>();
+            for (String fileName : fileNames) {
+                fileList.add(new File("D:/pdf/pdfToImage/"+fileName));
+            }
+            //去除PDF水印
+            removePdfWatermark(fileList);
+            //去除水印后的图片转PDF
+            List<Integer> fileNamesInteger = new ArrayList<>();
+            List<String> allFiles = new ArrayList<>();
+            for (String fileName : fileNames) {
+                if(fileName.endsWith(".png")){
+                    String str  = fileName.substring(0, fileName.indexOf(".png"));
+                    fileNamesInteger.add(Integer.valueOf(str));
+                }
+            }
+            Collections.sort(fileNamesInteger);
+            for (Integer fileName : fileNamesInteger) {
+                String str ="D:/pdf/pdfToImage/"+ fileName+".png";
+                allFiles.add(str);
+            }
+            String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
+            //图片转PDF
+            imgOfPdf("D:/pdf/imgToPdf/"+fileName, allFiles);
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void pdfToImage(String filePath) throws IOException {
+        File file = new File(filePath);
+        PDDocument doc = Loader.loadPDF(file);
+        PDFRenderer renderer = new PDFRenderer(doc);
+        int pageCount = doc.getNumberOfPages();
+        BufferedImage merge = null;
+        int curY = 0;
+        for (int i = 0; i < pageCount; i++) {
+            try {
+                BufferedImage image = renderer.renderImage(i, 2, ImageType.RGB);
+                ImageIO.write(image, "png", new File("D:/pdf/pdfToImage/" + i + ".png"));
+//                BufferedImage bufferedImage = renderer.renderImage(i, 2, ImageType.RGB);
+//                if (Objects.isNull(merge)) {
+//                    merge = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight() * i, BufferedImage.TYPE_INT_ARGB);
+//                }
+//                merge.getGraphics().drawImage(bufferedImage, 0, curY, null);
+//                curY += bufferedImage.getHeight();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+    /**
+     * 得到文件名称
+     *
+     * @param path 路径
+     * @return {@link List}<{@link String}>
+     */
+    private static List<String> getFileNames(String path) {
+        File file = new File(path);
+        if (!file.exists()) {
+            return null;
+        }
+        List<String> fileNames = new ArrayList<>();
+        return getFileNames(file, fileNames);
+    }
+
+    /**
+     * 得到文件名称
+     *
+     * @param file      文件
+     * @param fileNames 文件名
+     * @return {@link List}<{@link String}>
+     */
+    private static List<String> getFileNames(File file, List<String> fileNames) {
+        File[] files = file.listFiles();
+        for (File f : files) {
+            if (f.isDirectory()) {
+                getFileNames(f, fileNames);
+            } else {
+                fileNames.add(f.getName());
+            }
+        }
+        return fileNames;
+    }
+    /**
+     * 去除文件列表里图片的水印并替换
+     *
+     * @Param fileList 文件列表
+     */
+    public static void removePdfWatermark(List<File> fileList) {
+        try {
+            for (File file : fileList) {
+                if (!file.getName().endsWith("png") && !file.getName().endsWith("jpg")) {
+                    continue;
+                }
+                //用ImageIO流读取像素块
+                BufferedImage bi = ImageIO.read(file);
+                if (bi != null) {
+                    removeWatermark(bi);
+                    //生成的图片格式
+                    String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);
+                    //用ImageIO流生成的处理图替换原图片
+                    ImageIO.write(bi, formatName, file);
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+    /**
+     * 去除水印
+     */
+    private static void removeWatermark(BufferedImage bi) {
+        Color wColor = new Color(254, 254, 254);
+        Color hColor = new Color(197, 196, 191);
+        //白底水印
+        for (int i = 0; i < bi.getWidth(); i++) {
+            for (int j = 0; j < bi.getHeight(); 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 (red == 254 && greed == 254 && blue == 254) {
+                    continue;
+                }
+                if (red > 220 && greed > 180 && blue > 80) {
+                    bi.setRGB(i, j, wColor.getRGB());
+                }
+                if (red <= 240 && greed >= 200 && blue >= 150) {
+                    bi.setRGB(i, j, wColor.getRGB());
+                }
+            }
+        }
+    }
+    public static void imgOfPdf(String filepath, List<String> imgUrl) {
+
+        try {
+            //输出pdf文件路径
+            String pdfUrl =  filepath;
+            //生成pdf
+            File file = Pdf(imgUrl, pdfUrl);
+            file.createNewFile();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+    public static File Pdf(List<String> imageUrllist, String mOutputPdfFileName) {
+        //new一个pdf文档
+        Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
+        try {
+            //pdf写入
+            PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
+            //打开文档
+            doc.open();
+            //循环图片List,将图片加入到pdf中
+            for (int i = 0; i < imageUrllist.size(); i++) {
+                //在pdf创建一页
+                doc.newPage();
+                //通过文件路径获取image
+                Image png1 = Image.getInstance(imageUrllist.get(i));
+                float height = png1.getHeight();
+                float width = png1.getWidth();
+                int percent = getPercent2(height, width);
+                png1.setAlignment(Image.MIDDLE);
+                // 表示是原来图像的比例;
+                png1.scalePercent(percent + 3);
+                doc.add(png1);
+            }
+            doc.close();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (DocumentException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        //输出流
+        File mOutputPdfFile = new File(mOutputPdfFileName);
+        if (!mOutputPdfFile.exists()) {
+            mOutputPdfFile.deleteOnExit();
+            return null;
+        }
+        //反回文件输出流
+        return mOutputPdfFile;
+    }
+    public static int getPercent2(float h, float w) {
+        int p = 0;
+        float p2 = 0.0f;
+        p2 = 450 / w * 100;
+        p = Math.round(p2);
+        return p;
+    }
+}