MainFm.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.luooqi.ocr;
  2. import cn.hutool.core.lang.UUID;
  3. import com.luooqi.ocr.utils.CommUtils;
  4. import com.luooqi.ocr.utils.OcrUtils;
  5. import org.apache.commons.io.FileUtils;
  6. import javax.imageio.ImageIO;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.Collection;
  11. /**
  12. * 主窗体
  13. *
  14. * @author liuyuqi
  15. */
  16. public class MainFm {
  17. private static String outputDirPath;
  18. public static void main(String[] args) {
  19. String srcDirPath = "D:/liuyuqi/fishsource/python/video-subtitle-extract/videos/大象解说《血战钢锯岭》";
  20. outputDirPath = "D:/liuyuqi/fishsource/python/video-subtitle-extract/res";
  21. // 获取所有png文件
  22. Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"png"}, true);
  23. String res = "";
  24. for (File img : javaGbkFileCol) {
  25. // 对每个图片识别
  26. res += imgOCR(img) + "\r\n";
  27. }
  28. saveRes(res);
  29. System.out.println(res);
  30. }
  31. public static void saveRes(String res) {
  32. File file = new File(outputDirPath + Math.abs(UUID.randomUUID().hashCode()) + ".txt");
  33. try {
  34. FileUtils.writeStringToFile(file, res);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. public static String imgOCR(File img) {
  40. BufferedImage image;
  41. try {
  42. image = ImageIO.read(img);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. return "";
  46. }
  47. byte[] bytes = CommUtils.imageToBytes(image);
  48. int ocrType = 0;
  49. String text = OcrUtils.ocrImg(bytes, Math.abs(UUID.randomUUID().hashCode()) % 4);
  50. if (text == "") {
  51. text = OcrUtils.ocrImg(bytes, Math.abs(UUID.randomUUID().hashCode()) % 4);
  52. }
  53. return text;
  54. }
  55. }