| | |
| | | import com.opencsv.CSVReader; |
| | | import com.opencsv.CSVReaderBuilder; |
| | | import com.opencsv.exceptions.CsvValidationException; |
| | | 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.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.Pattern; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class TakeWords { |
| | |
| | | return result; |
| | | } |
| | | |
| | | public static Object readExcelFile(File file) throws FileNotFoundException, IOException { |
| | | public static Object readExcelFile(File file) throws IOException { |
| | | StringBuilder result = new StringBuilder(); |
| | | //创建工作簿对象 |
| | | XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file)); |
| | | XSSFWorkbook xssfWorkbook = new XSSFWorkbook(Files.newInputStream(file.toPath())); |
| | | //获取工作簿下sheet的个数 只读取第一个sheet |
| | | // int sheetNum = xssfWorkbook.getNumberOfSheets(); |
| | | //遍历工作簿中的所有数据 |
| | | for (int i = 0; i < 1; i++) { |
| | | XSSFSheet sheet = xssfWorkbook.getSheetAt(i); |
| | | //获取最后一行的num,即总行数。此处从0开始 |
| | | int maxRow = sheet.getLastRowNum(); |
| | | for (int row = 0; row <= maxRow; row++) { |
| | | //获取最后单元格num,即总单元格数 ***注意:此处从1开始计数*** |
| | | int maxRol = sheet.getRow(row).getLastCellNum(); |
| | | StringBuilder aLine = new StringBuilder(); |
| | | for (int rol = 0; rol < maxRol; rol++) { |
| | | aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier); |
| | | } |
| | | result.append(aLine).append("\n"); |
| | | XSSFSheet sheet = xssfWorkbook.getSheetAt(0); |
| | | //获取最后一行的num,即总行数。此处从0开始 |
| | | int maxRow = sheet.getLastRowNum(); |
| | | for (int row = 1; row <= maxRow; row++) { |
| | | //获取最后单元格num,即总单元格数 ***注意:此处从1开始计数*** |
| | | int maxRol = sheet.getRow(row).getLastCellNum(); |
| | | StringBuilder aLine = new StringBuilder(); |
| | | for (int rol = 0; rol < maxRol; rol++) { |
| | | aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier); |
| | | } |
| | | result.append(aLine).append("\n"); |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | public static Object readExcelxlsFile(File file) throws IOException { |
| | | StringBuilder result = new StringBuilder(); |
| | | String result = ""; |
| | | try (FileInputStream fis = new FileInputStream(file); |
| | | Workbook workbook = new HSSFWorkbook(fis)) { |
| | | // 获取第一个工作表 |
| | |
| | | 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); |
| | | // 调用 readPngFile1 方法读取图片文字信息 |
| | | String ocrResult = ""; |
| | | try { |
| | | ocrResult = (String) readPngFile1(tempFile); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | // ocrResult = (String) readPngFile(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(","); |
| | | //处理最后一张图片数据 |
| | | HSSFPictureData lastPicture = pictures.get(pictures.size()-1); |
| | | // 获取图片类型 |
| | | String pictureType = lastPicture.suggestFileExtension(); |
| | | // 获取图片数据 |
| | | byte[] pictureData = lastPicture.getData(); |
| | | // 创建临时文件 |
| | | File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType); |
| | | try (FileOutputStream fos = new FileOutputStream(tempFile)) { |
| | | fos.write(pictureData); |
| | | } |
| | | String ocrResult = ""; |
| | | try { |
| | | ocrResult = (String) readPngFile(tempFile); |
| | | } catch (TesseractException e) { |
| | | ocrResult = "OCR识别失败: " + e.getMessage(); |
| | | } finally { |
| | | // 删除临时文件 |
| | | tempFile.delete(); |
| | | } |
| | | result = ocrResult; |
| | | // String ocrText = fixOcrText(ocrResult); |
| | | // result.append("OCR Result:").append(ocrText).append(","); |
| | | |
| | | |
| | | // 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); |
| | | // // 调用 readPngFile1 方法读取图片文字信息 |
| | | // String ocrResult = ""; |
| | | // try { |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | //// ocrResult = (String) readPngFile(tempFile); |
| | | //// ocrResult = (String) readPngFile(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(","); |
| | | // } |
| | | } |
| | | // |
| | | // // 遍历每一行 |
| | |
| | | return tesseract.doOCR(file); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public static Object readTxtFile(File file) throws IOException { |
| | | FileInputStream fin = new FileInputStream(file); |
| | | InputStreamReader reader = new InputStreamReader(fin); |
| | |
| | | |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | // 创建 reader |
| | | // 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()) { |
| | | |
| | |
| | | } |
| | | tableMap.put("data", list); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | closeA1l(conn, preparedStatement, rs); |
| | | } |
| | |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | public static Object getmysqlFile(GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
| | | |
| | | public static Object getMysqlFile(GetFileDto getFileDto){ |
| | | Map<String, Object> tableMap = new HashMap<>(16); |
| | | // 数据库连接信息 |
| | | String url = "jdbc:mysql://localhost:3306/"+getFileDto.getDbFileName()+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; |
| | | // 从 GetFileDto 获取数据库名,对应【文件名称】字段 |
| | | String dbName = getFileDto.getDbFileName(); |
| | | String user = getFileDto.getDbUserName(); |
| | | String password = getFileDto.getDbPassword(); |
| | | List<ThicknessData> dataList = new ArrayList<>(); |
| | | // 从 GetFileDto 获取数据表名,对应【数据库表名】字段 |
| | | String table = getFileDto.getDbTable(); |
| | | // 检查数据库名和表名是否为空 |
| | | if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) { |
| | | return R.failed("数据库名或表名不能为空"); |
| | | } |
| | | // 数据库连接信息 |
| | | String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&characterEncoding=utf8"; |
| | | Connection connection = null; |
| | | PreparedStatement preparedStatement = null; |
| | | ResultSet resultSet = null; |
| | | List<Map<String, Object>> 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); |
| | | try { |
| | | // 建立连接 |
| | | connection = DriverManager.getConnection(url, user, password); |
| | | // 构建基础 SQL |
| | | |
| | | String sql = "SELECT * FROM "+table+" WHERE 1=1"; |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode())){ |
| | | sql+=" AND " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode()+ "'"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){ |
| | | sql+=" AND " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'"; |
| | | } |
| | | // 创建 PreparedStatement 对象执行 SQL |
| | | preparedStatement = connection.prepareStatement(sql); |
| | | resultSet = preparedStatement.executeQuery(); |
| | | ResultSetMetaData metaData = resultSet.getMetaData(); |
| | | int columnCount = metaData.getColumnCount(); |
| | | // 遍历结果集获取数据 |
| | | while (resultSet.next()) { |
| | | double thinnestPoint = resultSet.getDouble("ThinnestPoint"); |
| | | double averageThickness = resultSet.getDouble("AverageThickness"); |
| | | dataList.add(new ThicknessData(thinnestPoint, averageThickness)); |
| | | Map<String, Object> rowData = new HashMap<>(); |
| | | for (int i = 1; i <= columnCount; i++) { |
| | | String columnName = metaData.getColumnName(i); |
| | | rowData.put(columnName, resultSet.getObject(i)); |
| | | } |
| | | dataList.add(rowData); |
| | | } |
| | | tableMap.put("data", dataList); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 假设 R 类有 failed 方法,若没有需补充实现 |
| | | return R.failed("数据库查询出错: " + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | if (resultSet != null) { |
| | | resultSet.close(); |
| | | } |
| | | if (preparedStatement != null) { |
| | | preparedStatement.close(); |
| | | } |
| | | if (connection != null) { |
| | | connection.close(); |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return tableMap; |
| | | } |