| | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import com.chinaztt.mes.docx.dto.GetFileDto; |
| | | import com.chinaztt.mes.docx.dto.ThicknessData; |
| | | import com.opencsv.CSVReader; |
| | | import com.opencsv.CSVReaderBuilder; |
| | | import com.opencsv.exceptions.CsvValidationException; |
| | | 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.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | 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 { |
| | | |
| | | private static final String splitIdentifier = "@-@"; // 自定义唯一标识符 |
| | | |
| | | // 科学计数法正则模式 |
| | | private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile( |
| | | "^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$" |
| | | ); |
| | | |
| | | public static Object readWordFile(File file) { |
| | | String result = ""; |
| | |
| | | fos.write(pictureData); |
| | | } |
| | | // 图片预处理 |
| | | File processedFile = preprocessImage(tempFile, pictureType); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | |
| | | // File processedFile = preprocessImage(tempFile, pictureType); |
| | | // 调用 readPngFile1 方法读取图片文字信息 |
| | | String ocrResult = ""; |
| | | try { |
| | | ocrResult = (String) readPngFile1(tempFile); |
| | | // ocrResult = (String) readPngFile1(processedFile); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | // ocrResult = (String) readPngFile(processedFile); |
| | | } catch (TesseractException e) { |
| | | ocrResult = "OCR识别失败: " + e.getMessage(); |
| | | } finally { |
| | | // 删除临时文件 |
| | | tempFile.delete(); |
| | | processedFile.delete(); |
| | | // processedFile.delete(); |
| | | } |
| | | |
| | | // 将图片信息添加到结果中 |
| | |
| | | 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"); |
| | |
| | | |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | // 创建 reader |
| | | try (BufferedReader br = Files.newBufferedReader(file.toPath())) { |
| | | // CSV文件的分隔符 |
| | | String DELIMITER = ","; |
| | | // 按行读取 |
| | | String line; |
| | | while ((line = br.readLine()) != null) { |
| | | // 分割 |
| | | String[] columns = line.split(DELIMITER); |
| | | // 打印行 |
| | | stringBuilder.append(String.join(splitIdentifier, columns)).append("\n"); |
| | | // try (BufferedReader br = Files.newBufferedReader(file.toPath())) { |
| | | // // CSV文件的分隔符 |
| | | // String DELIMITER = ","; |
| | | // // 按行读取 |
| | | // String line; |
| | | // System.out.println(br.readLine()); |
| | | // while ((line = br.readLine()) != null) { |
| | | // // 分割 |
| | | // String[] columns = line.split(DELIMITER); |
| | | // // 打印行 |
| | | // stringBuilder.append(String.join(splitIdentifier, columns)).append("\n"); |
| | | // } |
| | | // } catch (IOException ex) { |
| | | // ex.printStackTrace(); |
| | | // } |
| | | try (FileReader fileReader = new FileReader(file); |
| | | CSVReader csvReader = new CSVReaderBuilder(fileReader).build()) { |
| | | |
| | | String[] nextLine; |
| | | while ((nextLine = csvReader.readNext()) != null) { |
| | | // 处理每一行数据 |
| | | for (String cell : nextLine) { |
| | | if(StringUtils.isNotBlank(cell)){ |
| | | stringBuilder.append(scientificToNumber(cell)).append(splitIdentifier); |
| | | } |
| | | } |
| | | stringBuilder.append("\n"); |
| | | } |
| | | } catch (IOException ex) { |
| | | ex.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (CsvValidationException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return stringBuilder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 将科学计数法转换为数字 |
| | | * @param cell |
| | | * @return |
| | | */ |
| | | public static String scientificToNumber(String cell){ |
| | | if(SCIENTIFIC_PATTERN.matcher(cell).matches()){ |
| | | return String.valueOf(Double.parseDouble(cell)); |
| | | } |
| | | return cell; |
| | | } |
| | | |
| | | public static Object readMdbFile(File file, GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
| | |
| | | } else { |
| | | path = canonicalPath64.replaceAll("/chi_sim.traineddata", "").replaceAll("\\\\", "/"); |
| | | } |
| | | // 设置 TESSDATA_PREFIX 环境变量 |
| | | // System.setProperty("TESSDATA_PREFIX", path); |
| | | //设置配置文件夹微视、识别语言、识别模式 |
| | | Tesseract tesseract = new Tesseract(); |
| | | tesseract.setDatapath(path); |