初始化项目

This commit is contained in:
2026-01-20 17:16:09 +08:00
parent 078f1cd7db
commit a9ea56a42d
8 changed files with 118 additions and 0 deletions

View File

@@ -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>

View 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;
}
}

View 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);
}
}

View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
src/main/resources/b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

Binary file not shown.