feat: OCR接口
This commit is contained in:
@@ -2,7 +2,9 @@ package com.klp.controller;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.klp.service.ITesseractOcrService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.*;
|
||||||
@@ -35,6 +37,7 @@ import com.klp.common.core.page.TableDataInfo;
|
|||||||
public class WmsPurchasePlanController extends BaseController {
|
public class WmsPurchasePlanController extends BaseController {
|
||||||
|
|
||||||
private final IWmsPurchasePlanService iWmsPurchasePlanService;
|
private final IWmsPurchasePlanService iWmsPurchasePlanService;
|
||||||
|
private final ITesseractOcrService iTesseractOcrService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增采购计划(含明细)
|
* 新增采购计划(含明细)
|
||||||
@@ -52,6 +55,17 @@ public class WmsPurchasePlanController extends BaseController {
|
|||||||
return R.ok(planVo);
|
return R.ok(planVo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 识别图片中的文字
|
||||||
|
* @param request 图片地址
|
||||||
|
* @return 识别出的文字
|
||||||
|
*/
|
||||||
|
@PostMapping("/recognizeText")
|
||||||
|
public R<String> recognize(@RequestBody Map<String, String> request) {
|
||||||
|
String imgUrl = request.get("imgUrl");
|
||||||
|
return R.ok(iTesseractOcrService.recognizeText(imgUrl));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询采购计划主列表
|
* 查询采购计划主列表
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ public interface ITesseractOcrService {
|
|||||||
* @param imageUrl 图片URL
|
* @param imageUrl 图片URL
|
||||||
* @return 识别结果
|
* @return 识别结果
|
||||||
*/
|
*/
|
||||||
String recognizeText(String imageUrl) throws Exception;
|
String recognizeText(String imageUrl);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import com.klp.common.config.TesseractConfig;
|
|||||||
import com.klp.service.ITesseractOcrService;
|
import com.klp.service.ITesseractOcrService;
|
||||||
import net.sourceforge.tess4j.ITesseract;
|
import net.sourceforge.tess4j.ITesseract;
|
||||||
import net.sourceforge.tess4j.Tesseract;
|
import net.sourceforge.tess4j.Tesseract;
|
||||||
|
import net.sourceforge.tess4j.TesseractException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
@@ -19,12 +21,17 @@ public class TesseractOcrServiceImpl implements ITesseractOcrService {
|
|||||||
@Resource
|
@Resource
|
||||||
private TesseractConfig tesseractConfig;
|
private TesseractConfig tesseractConfig;
|
||||||
@Override
|
@Override
|
||||||
public String recognizeText(String imageUrl) throws Exception {
|
public String recognizeText(String imageUrl){
|
||||||
// 读取网络图片为 BufferedImage
|
// 读取网络图片为 BufferedImage
|
||||||
URL url = new URL(imageUrl);
|
BufferedImage image = null;
|
||||||
InputStream inputStream = url.openStream();
|
try {
|
||||||
BufferedImage image = ImageIO.read(inputStream);
|
URL url = new URL(imageUrl);
|
||||||
inputStream.close();
|
InputStream inputStream = url.openStream();
|
||||||
|
image = ImageIO.read(inputStream);
|
||||||
|
inputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
// 预处理图片
|
// 预处理图片
|
||||||
BufferedImage bufferedImage = preprocessImage(image);
|
BufferedImage bufferedImage = preprocessImage(image);
|
||||||
System.out.println("开始OCR识别...");
|
System.out.println("开始OCR识别...");
|
||||||
@@ -38,7 +45,12 @@ public class TesseractOcrServiceImpl implements ITesseractOcrService {
|
|||||||
tesseract.setPageSegMode(6); // 假设统一的文本块
|
tesseract.setPageSegMode(6); // 假设统一的文本块
|
||||||
tesseract.setOcrEngineMode(3); // 使用默认引擎
|
tesseract.setOcrEngineMode(3); // 使用默认引擎
|
||||||
// 执行OCR识别图片
|
// 执行OCR识别图片
|
||||||
String result = tesseract.doOCR(bufferedImage);
|
String result = null;
|
||||||
|
try {
|
||||||
|
result = tesseract.doOCR(bufferedImage);
|
||||||
|
} catch (TesseractException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
// 清理和格式化结果
|
// 清理和格式化结果
|
||||||
String cleanedResult = cleanOcrResult(result);
|
String cleanedResult = cleanOcrResult(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user