Tess4jClient.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.ruixuan.isc.utils;
  2. import io.minio.GetObjectArgs;
  3. import lombok.Data;
  4. import net.sourceforge.tess4j.ITesseract;
  5. import net.sourceforge.tess4j.Tesseract;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import javax.imageio.ImageIO;
  11. import java.awt.image.BufferedImage;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. @Data
  17. @Component
  18. public class Tess4jClient {
  19. private String dataPath = "/data/webapp/RuiXuan-Cloud/tessdata";
  20. private String language = "chi_sim";
  21. // 入参:图片流
  22. public String doOCR(BufferedImage image) throws Exception {
  23. //创建Tesseract对象
  24. ITesseract tesseract = new Tesseract();
  25. //设置中文字体库路径
  26. tesseract.setDatapath(dataPath);
  27. //中文识别
  28. tesseract.setLanguage(language);
  29. //执行ocr识别
  30. String result = tesseract.doOCR(image);
  31. //替换回车和tal键 使结果为一行
  32. System.out.println(result.replaceAll(" ", ""));
  33. result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
  34. return result;
  35. }
  36. public String getWords(MultipartFile file) throws Exception {
  37. InputStream inputStream = file.getInputStream();
  38. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  39. byte[] buff = new byte[100];
  40. int rc = 0;
  41. while (true) {
  42. try {
  43. if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. byteArrayOutputStream.write(buff, 0, rc);
  48. }
  49. //从byte[]转换为butteredImage
  50. ByteArrayInputStream in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  51. BufferedImage imageFile = ImageIO.read(in);
  52. //识别图片的文字
  53. return doOCR(imageFile);
  54. //再结合敏感词过滤算法,审核图片中的文字是否包含敏感词
  55. }
  56. // public byte[] downLoadFile(String pathUrl) {
  57. // String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");
  58. // int index = key.indexOf("/");
  59. // String bucket = key.substring(0,index);
  60. // String filePath = key.substring(index+1);
  61. // InputStream inputStream = null;
  62. // try {
  63. // inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
  64. // } catch (Exception e) {
  65. // log.error("minio down file error. pathUrl:{}",pathUrl);
  66. // e.printStackTrace();
  67. // }
  68. //
  69. // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  70. // byte[] buff = new byte[100];
  71. // int rc = 0;
  72. // while (true) {
  73. // try {
  74. // if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
  75. // } catch (IOException e) {
  76. // e.printStackTrace();
  77. // }
  78. // byteArrayOutputStream.write(buff, 0, rc);
  79. // }
  80. // return byteArrayOutputStream.toByteArray();
  81. // }
  82. }