OcrService.java 945 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.jeesite.modules.mes.service;
  2. import lombok.AllArgsConstructor;
  3. import net.sourceforge.tess4j.Tesseract;
  4. import net.sourceforge.tess4j.TesseractException;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import javax.imageio.ImageIO;
  8. import java.awt.image.BufferedImage;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. @Service
  13. @AllArgsConstructor
  14. public class OcrService {
  15. private final Tesseract tesseract;
  16. /*
  17. * 识别图片中的数据
  18. * @param imageFile 图片文件
  19. * @ return 识别信息
  20. * */
  21. public String recognizeText(MultipartFile imageFile) throws TesseractException, IOException {
  22. // 转换
  23. InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
  24. BufferedImage bufferedImage = ImageIO.read(sbs);
  25. return tesseract.doOCR(bufferedImage);
  26. }
  27. }