初始化项目
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -48,6 +48,13 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Tess4J 依赖 -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.tess4j</groupId>
|
||||
<artifactId>tess4j</artifactId>
|
||||
<version>5.18.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
18
src/main/java/com/tem/bocai/config/TessConfig.java
Normal file
18
src/main/java/com/tem/bocai/config/TessConfig.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.tem.bocai.config;
|
||||
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class TessConfig {
|
||||
|
||||
@Bean
|
||||
public Tesseract tesseract() {
|
||||
|
||||
Tesseract instance = new Tesseract();
|
||||
instance.setLanguage("oci"); // 设置语言包,这里使用英语
|
||||
instance.setDatapath("src/main/resources/tessdata"); // 设置语言包路径
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
37
src/main/java/com/tem/bocai/controller/TestController.java
Normal file
37
src/main/java/com/tem/bocai/controller/TestController.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.tem.bocai.controller;
|
||||
|
||||
import com.tem.bocai.util.ImageOcrService;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
|
||||
private final ImageOcrService imageOcrService;
|
||||
|
||||
@Autowired
|
||||
public TestController(ImageOcrService imageOcrService) {
|
||||
|
||||
this.imageOcrService = imageOcrService;
|
||||
}
|
||||
|
||||
@GetMapping("/ocr/local")
|
||||
public ResponseEntity<String> ocrLocalImage(String imagePath) throws IOException, TesseractException {
|
||||
imagePath = "b.jpg";
|
||||
String result = imageOcrService.ocrLocalImage(imagePath);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@GetMapping("/ocr/remote")
|
||||
public ResponseEntity<String> ocrRemoteImage(String imageUrl) throws IOException, TesseractException {
|
||||
|
||||
String result = imageOcrService.ocrRemoteImage(imageUrl);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
}
|
||||
56
src/main/java/com/tem/bocai/util/ImageOcrService.java
Normal file
56
src/main/java/com/tem/bocai/util/ImageOcrService.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.tem.bocai.util;
|
||||
|
||||
import net.sourceforge.tess4j.Tesseract;
|
||||
import net.sourceforge.tess4j.TesseractException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Service
|
||||
public class ImageOcrService {
|
||||
|
||||
|
||||
private final Tesseract tesseract;
|
||||
private final ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
public ImageOcrService(Tesseract tesseract, ResourceLoader resourceLoader) {
|
||||
|
||||
this.tesseract = tesseract;
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地文件路径读取图片并进行 OCR 处理
|
||||
*
|
||||
* @param imagePath 图片文件路径
|
||||
* @return OCR 结果文本
|
||||
*/
|
||||
public String ocrLocalImage(String imagePath) throws IOException, TesseractException {
|
||||
|
||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(imagePath);
|
||||
BufferedImage image = ImageIO.read(inputStream);
|
||||
return tesseract.doOCR(image);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从远程 URL 获取图片并进行 OCR 处理
|
||||
*
|
||||
* @param imageUrl 图片 URL
|
||||
* @return OCR 结果文本
|
||||
*/
|
||||
public String ocrRemoteImage(String imageUrl) throws IOException, TesseractException {
|
||||
|
||||
byte[] imageData = IOUtils.toByteArray(new ByteArrayInputStream(IOUtils.toByteArray(imageUrl)));
|
||||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
|
||||
return tesseract.doOCR(image);
|
||||
}
|
||||
}
|
||||
BIN
src/main/resources/a.jpg
Normal file
BIN
src/main/resources/a.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
src/main/resources/b.jpg
Normal file
BIN
src/main/resources/b.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.8 KiB |
BIN
src/main/resources/tessdata/eng.traineddata
Normal file
BIN
src/main/resources/tessdata/eng.traineddata
Normal file
Binary file not shown.
BIN
src/main/resources/tessdata/oci.traineddata
Normal file
BIN
src/main/resources/tessdata/oci.traineddata
Normal file
Binary file not shown.
Reference in New Issue
Block a user