| | |
| | | import com.ruoyi.common.core.controller.BaseController; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.utils.file.FileUploadUtils; |
| | | import com.ruoyi.common.utils.image.ImageCompressUtil; |
| | | import com.ruoyi.system.utils.AliOCRUtil; |
| | | import com.ruoyi.system.utils.BaiduOCRUtil; |
| | | import com.ruoyi.system.utils.TencentOCRUtil; |
| | |
| | | return error("不支持的识别类型: " + type + ", 支持的类型: " + String.join(",", SUPPORTED_TYPES)); |
| | | } |
| | | |
| | | // 保存临时文件 |
| | | String tempDir = System.getProperty("java.io.tmpdir"); |
| | | String originalFilename = file.getOriginalFilename(); |
| | | File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + originalFilename); |
| | | file.transferTo(tempFile); |
| | | // 智能压缩图片(自动处理超过3MB的图片) |
| | | File tempFile = ImageCompressUtil.compressForOCR(file); |
| | | |
| | | // 根据提供商调用不同的OCR服务 |
| | | JSONObject ocrResult; |
| | |
| | | // 构建返回结果 |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("ocrResult", ocrResult); |
| | | result.put("fileName", originalFilename); |
| | | result.put("fileName", file.getOriginalFilename()); |
| | | result.put("originalSize", file.getSize()); |
| | | result.put("processedSize", tempFile.length()); |
| | | result.put("compressed", file.getSize() > tempFile.length()); |
| | | result.put("type", type); |
| | | result.put("provider", provider); |
| | | |
| | |
| | | return error("上传图片不能为空"); |
| | | } |
| | | |
| | | // 保存临时文件 |
| | | String tempDir = System.getProperty("java.io.tmpdir"); |
| | | String originalFilename = file.getOriginalFilename(); |
| | | File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + originalFilename); |
| | | file.transferTo(tempFile); |
| | | // 智能压缩图片(自动处理超过3MB的图片) |
| | | File tempFile = ImageCompressUtil.compressForOCR(file); |
| | | |
| | | // 调用腾讯云手写体识别 |
| | | Map<String, String> resultMap = TencentOCRUtil.handwritingRecognizeWith(tempFile.getAbsolutePath(), itemNames); |
| | |
| | | |
| | | // 构建返回结果 |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("fileName", originalFilename); |
| | | result.put("fileName", file.getOriginalFilename()); |
| | | result.put("type", "HandWriting"); |
| | | result.put("provider", "tencent"); |
| | | result.put("fields", resultMap); |
| | |
| | | } |
| | | |
| | | try { |
| | | // 保存临时文件 |
| | | String tempDir = System.getProperty("java.io.tmpdir"); |
| | | String originalFilename = file.getOriginalFilename(); |
| | | File tempFile = new File(tempDir, System.currentTimeMillis() + "_" + originalFilename); |
| | | file.transferTo(tempFile); |
| | | // 智能压缩图片(自动处理超过3MB的图片) |
| | | File tempFile = ImageCompressUtil.compressForOCR(file); |
| | | |
| | | // 调用腾讯云手写体识别 |
| | | Map<String, String> resultMap = TencentOCRUtil.handwritingRecognizeWith(tempFile.getAbsolutePath(), itemNames); |
| | |
| | | // 检查是否有错误 |
| | | if (resultMap.containsKey("error")) { |
| | | failCount++; |
| | | errorMessages.append(originalFilename).append(":").append(resultMap.get("error")).append("; "); |
| | | logger.warn("图片 {} 识别失败: {}", originalFilename, resultMap.get("error")); |
| | | errorMessages.append(file.getOriginalFilename()).append(":").append(resultMap.get("error")).append("; "); |
| | | logger.warn("图片 {} 识别失败: {}", file.getOriginalFilename(), resultMap.get("error")); |
| | | } else { |
| | | // 合并识别结果(如果key已存在,不覆盖) |
| | | for (Map.Entry<String, String> entry : resultMap.entrySet()) { |
| | |
| | | } |
| | | } |
| | | successCount++; |
| | | logger.info("图片 {} 识别成功,提取 {} 个字段", originalFilename, resultMap.size()); |
| | | logger.info("图片 {} 识别成功,提取 {} 个字段", file.getOriginalFilename(), resultMap.size()); |
| | | } |
| | | } catch (Exception e) { |
| | | failCount++; |