yaowanxin
2025-07-19 2d1e909070e2baaa49d3a021364df595ff1d2e72
src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
@@ -2,22 +2,34 @@
import cn.hutool.core.io.FileUtil;
import com.chinaztt.mes.docx.dto.GetFileDto;
import com.chinaztt.mes.docx.dto.ThicknessData;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.nio.file.Files;
import java.sql.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TakeWords {
@@ -68,6 +80,205 @@
        return result.toString();
    }
    public static Object readExcelxlsFile(File file) throws IOException {
        StringBuilder result = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(file);
             Workbook workbook = new HSSFWorkbook(fis)) {
            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 读取图片信息
            if (workbook instanceof HSSFWorkbook) {
                HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook;
                List<HSSFPictureData> pictures = hssfWorkbook.getAllPictures();
                for (HSSFPictureData picture : pictures) {
                    // 获取图片类型
                    String pictureType = picture.suggestFileExtension();
                    // 获取图片数据
                    byte[] pictureData = picture.getData();
                    // 创建临时文件
                    File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType);
                    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                        fos.write(pictureData);
                    }
                    // 图片预处理
                    File processedFile = preprocessImage(tempFile, pictureType);
//                        ocrResult = (String) readPngFile(tempFile);
                    // 调用 readPngFile1 方法读取图片文字信息
                    String ocrResult = "";
                    try {
                        ocrResult = (String) readPngFile1(tempFile);
//                        ocrResult = (String) readPngFile1(processedFile);
                    } catch (TesseractException e) {
                        ocrResult = "OCR识别失败: " + e.getMessage();
                    } finally {
                        // 删除临时文件
                        tempFile.delete();
                        processedFile.delete();
                    }
                    // 将图片信息添加到结果中
//                    result.append("Picture Type: ").append(pictureType)
//                            .append(", Picture Size: ").append(pictureData.length)
//                            .append(" bytes")
//                            .append(", OCR Result: ").append(ocrResult)
//                            .append(",");
                    String ocrText = fixOcrText(ocrResult);
                    result.append("OCR Result:").append(ocrText).append(",");
                }
            }
//
//            // 遍历每一行
//            for (Row row : sheet) {
//                // 遍历每一列
//                for (Cell cell : row) {
//                    CellType cellType = CellType.forInt(cell.getCellType());
//                    switch (cellType) {
//                        case STRING:
//                            result.append(cell.getStringCellValue());
//                            break;
//                        case NUMERIC:
//                            if (DateUtil.isCellDateFormatted(cell)) {
//                                result.append(cell.getDateCellValue());
//                            } else {
//                                result.append(cell.getNumericCellValue());
//                            }
//                            break;
//                        case BOOLEAN:
//                            result.append(cell.getBooleanCellValue());
//                            break;
//                        case FORMULA:
//                            result.append(cell.getCellFormula());
//                            break;
//                        default:
//                            result.append("");
//                    }
//                    result.append("\t");
//                }
//                result.append("\n");
//            }
        }
        return result;
    }
    // 修正 OCR 识别文本中的错误关键词
    public static String fixOcrText(String ocrText) {
        // 定义错误关键词和正确内容的映射,这里处理“击 宇 强 庞”修正为“击穿强度”
        // 考虑到可能有空格分隔,用正则匹配包含这些字的内容
        ocrText = ocrText.replaceAll("击\\s*宇\\s*强\\s*庞", "击穿强度");
        // 还可以继续添加其他错误修正,比如下面假设“电 压 \\(HV\\)”里的空格影响,也修正下
        ocrText = ocrText.replaceAll("电\\s*压\\s*\\(HV\\)", "电压(KV)");
        ocrText = ocrText.replaceAll("电\\s*流\\s*\\(nt\\)", "电流(mA)");
        return ocrText;
    }
    /**
     * 对图片进行预处理,包括灰度化、二值化和锐化
     * @param inputFile 输入的图片文件
     * @param formatName 图片格式名称
     * @return 处理后的图片文件
     * @throws IOException 读取或写入图片时可能抛出的异常
     */
    private static File preprocessImage(File inputFile, String formatName) throws IOException {
        // 读取图片
        BufferedImage image = ImageIO.read(inputFile);
        // 灰度化
        image = convertToGrayscale(image);
        // 二值化
        image = applyThreshold(image, 128);
        // 锐化
        image = applySharpening(image);
        // 创建处理后的临时文件
        File outputFile = File.createTempFile(UUID.randomUUID().toString(), "." + formatName);
        ImageIO.write(image, formatName, outputFile);
        return outputFile;
    }
    /**
     * 将图片转换为灰度图
     * @param image 输入的图片
     * @return 灰度化后的图片
     */
    private static BufferedImage convertToGrayscale(BufferedImage image) {
        BufferedImage grayImage = new BufferedImage(
                image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        grayImage.getGraphics().drawImage(image, 0, 0, null);
        return grayImage;
    }
    /**
     * 对图片进行二值化处理
     * @param image 输入的图片
     * @param threshold 二值化阈值
     * @return 二值化后的图片
     */
    private static BufferedImage applyThreshold(BufferedImage image, int threshold) {
        BufferedImage binaryImage = new BufferedImage(
                image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                int rgb = image.getRGB(x, y);
                int gray = (rgb >> 16) & 0xff;
                if (gray < threshold) {
                    binaryImage.setRGB(x, y, Color.BLACK.getRGB());
                } else {
                    binaryImage.setRGB(x, y, Color.WHITE.getRGB());
                }
            }
        }
        return binaryImage;
    }
    /**
     * 对图片进行锐化处理
     * @param image 输入的图片
     * @return 锐化后的图片
     */
    private static BufferedImage applySharpening(BufferedImage image) {
        float[] sharpenMatrix = {
                0f, -1f, 0f,
                -1f, 5f, -1f,
                0f, -1f, 0f
        };
        java.awt.image.Kernel kernel = new java.awt.image.Kernel(3, 3, sharpenMatrix);
        java.awt.image.ConvolveOp op = new java.awt.image.ConvolveOp(kernel, java.awt.image.ConvolveOp.EDGE_NO_OP, null);
        return op.filter(image, null);
    }
    public static Object readPngFile1(File file) throws IOException, TesseractException {
        // 获取 tessdata 目录的绝对路径
        String arch = System.getProperty("sun.arch.data.model");
        File tessDataDir;
        if (arch.contains("32")) {
            tessDataDir = FileUtil.file(".", "/jre_32/tessdata");
        } else {
            tessDataDir = FileUtil.file(".", "/jre_64/tessdata");
        }
        String path = tessDataDir.getCanonicalPath();
        // 检查 chi_sim.traineddata 文件是否存在
        File chiSimFile = new File(path, "chi_sim.traineddata");
        if (!chiSimFile.exists()) {
            throw new FileNotFoundException("chi_sim.traineddata 文件未找到,请检查路径: " + chiSimFile.getAbsolutePath());
        }
        // 设置配置文件夹、识别语言、识别模式
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath(path);
        // 设置识别语言为中文简体和英文(如果要设置为英文可改为 "eng")
        tesseract.setLanguage("chi_sim+eng");
        // 使用 OSD 进行自动页面分割以进行图像处理
        tesseract.setPageSegMode(1);
        // 设置引擎模式是神经网络 LSTM 引擎
        tesseract.setOcrEngineMode(1);
        // 开始识别整张图片中的文字
        return tesseract.doOCR(file);
    }
    public static Object readTxtFile(File file) throws IOException {
        FileInputStream fin = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fin);
@@ -82,6 +293,7 @@
    }
    public static Object readCsvFile(File file) {
        StringBuilder stringBuilder = new StringBuilder();
        // 创建 reader
        try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
@@ -164,6 +376,36 @@
        } catch (Exception ignore) {
        }
    }
    public static Object getmysqlFile(GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Map<String, Object> tableMap = new HashMap<>(16);
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/"+getFileDto.getDbFileName()+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
        String user = getFileDto.getDbUserName();
        String password = getFileDto.getDbPassword();
        List<ThicknessData> dataList = new ArrayList<>();
        try (
                // 建立连接
                Connection connection = DriverManager.getConnection(url, user, password);
                // 创建 Statement 对象执行 SQL
                Statement statement = connection.createStatement()
        ) {
            String sql = "SELECT ThinnestPoint, AverageThickness FROM model1records";
            ResultSet resultSet = statement.executeQuery(sql);
            // 遍历结果集获取数据
            while (resultSet.next()) {
                double thinnestPoint = resultSet.getDouble("ThinnestPoint");
                double averageThickness = resultSet.getDouble("AverageThickness");
                dataList.add(new ThicknessData(thinnestPoint, averageThickness));
            }
            tableMap.put("data", dataList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("数据库查询出错: " + e.getMessage());
        }
        return tableMap;
    }
    public static Object readDbFile(File file, GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Map<String, Object> tableMap = new HashMap<>(16);