MainFm.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //
  21. String outputDirPath = "D:/liuyuqi/fishsource/python/video-subtitle-extract/";
  22. // 获取所有png文件
  23. Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"png"}, true);
  24. String res = "";
  25. for (File img : javaGbkFileCol) {
  26. // 对每个图片识别
  27. res += imgOCR(img);
  28. }
  29. saveRes(res);
  30. System.out.println(res);
  31. }
  32. public static void saveRes(String res){
  33. File file = new File(outputDirPath+Math.abs(UUID.randomUUID().hashCode())+".txt");
  34. try {
  35. FileUtils.writeStringToFile(file,res);
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. public static String imgOCR(File img) {
  41. BufferedImage image;
  42. try {
  43. image = ImageIO.read(img);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. return "";
  47. }
  48. byte[] bytes = CommUtils.imageToBytes(image);
  49. int ocrType = 0;
  50. ocrType = Math.abs(UUID.randomUUID().hashCode()) % 4;
  51. String text = OcrUtils.ocrImg(bytes, ocrType);
  52. return text;
  53. }
  54. }