当前位置: 首页 > news >正文

网上商店网站设计临沂品牌网站推广

网上商店网站设计,临沂品牌网站推广,做投票的网站,西地那非片功效与作用主要会有哪些需求&#xff1a;识别pdf文件中的中文 根据github项目mymonstercat 改造,先将pdf文件转为png文件存于临时文件夹&#xff0c;然后通过RapidOcr转为文字,最后删除临时文件夹 1、引入依赖 <dependency><groupId>org.apache.pdfbox</groupId><artifactId&g…

需求:识别pdf文件中的中文

根据github项目mymonstercat 改造,先将pdf文件转为png文件存于临时文件夹,然后通过RapidOcr转为文字,最后删除临时文件夹

1、引入依赖

		<dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>3.0.3</version></dependency><!-- ocr图片识别 --><dependency><groupId>io.github.mymonstercat</groupId><artifactId>rapidocr</artifactId><version>0.0.7</version></dependency><dependency><groupId>io.github.mymonstercat</groupId><artifactId>rapidocr-onnx-platform</artifactId><version>0.0.7</version></dependency><!-- 本地测试可不引 , 服务器部署linux x86架构 下引入 ,其他环境部署可搜maven --><dependency><groupId>io.github.mymonstercat</groupId><artifactId>rapidocr-onnx-linux-x86_64</artifactId><version>1.2.2</version></dependency>

2、工具类

import org.springframework.util.StringUtils;
import com.benjaminwan.ocrlibrary.OcrResult;
import com.benjaminwan.ocrlibrary.TextBlock;import io.github.mymonstercat.Model;
import io.github.mymonstercat.ocr.InferenceEngine;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Service;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
@Service
public class PdfOCRConverter {//临时输出png文件路径private static final String outputDirs = "D:/pdfToImg/temp/";public static void main(String[] args) throws IOException {List<String> fileNameList = getWords("D:/Download/123.pdf");for (String fileName : fileNameList) {System.out.println(fileName);}}public static List<String> getWords(String pdfFilePath) throws IOException {String outputDir =  outputDirs + UUID.randomUUID().toString().replace("-", "");List<String> fileNameList = convertPdfToImage(pdfFilePath, outputDir);List<String> wordsList = new ArrayList<>();for (String fileName : fileNameList) {System.out.println("识别图片:"+fileName);if (StringUtils.isEmpty(fileName)){break;}List<String> words = runOcr(fileName);for (String word : words) {System.out.println(word);wordsList.add(word);}}deleteDirectory(outputDir);return wordsList;}public static List<String> runOcr(String path) {List<String> results = new ArrayList<>();InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);OcrResult ocrResult = engine.runOcr(path);for (TextBlock textBlock : ocrResult.getTextBlocks()) {results.add(textBlock.getText());}return results;}public static List<String> convertPdfToImage(String pdfFilePath, String outputDir) {// 设置DPI(越高图片越清晰,但文件也会更大)int dpi = 300;List<String> fileNameList = new ArrayList<>();File file = new File(pdfFilePath);try (PDDocument document = Loader.loadPDF(file)) {PDFRenderer pdfRenderer = new PDFRenderer(document);String pdfFileName = file.getName().replace(".pdf", "");String name = pdfFileName;for (int page = 0; page < document.getNumberOfPages(); page++) {BufferedImage bim = pdfRenderer.renderImageWithDPI(page, dpi);String folder = createFolder(outputDir + "/" + name);String fileName = folder + "/" + pdfFileName + "_page_" + (page + 1) + ".png";ImageIO.write(bim, "png", new File(fileName));fileNameList.add(fileName);System.out.println("生成图片:"+fileName);}} catch (IOException e) {e.printStackTrace();}return fileNameList;}public static void deleteDirectory(String path) throws IOException {// 如果路径不指向一个目录,则抛出异常Path directory = Paths.get(path);if (!Files.isDirectory(directory)) {throw new IOException("The provided path is not a directory.");}// 遍历目录中的所有文件和子目录Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {// 删除文件Files.delete(file);return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {// 所有内容被删除后删除目录本身Files.delete(dir);return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {// 如果访问文件失败,则抛出异常throw exc;}});}public static String createFolder(String folderPath) {String txt = folderPath;try {File myFilePath = new File(txt);txt = folderPath;if (!myFilePath.exists()) {myFilePath.mkdirs();}} catch (Exception e) {e.printStackTrace();}return txt;}public static List<String> getWordsByBase64(String base64) throws IOException {List<String> words = new ArrayList<>();if (StringUtils.isEmpty(base64)) {return null;}String outputDir = outputDirs + UUID.randomUUID().toString().replace("-", "");// 解码Base64字符串byte[] decodedBytes = Base64.getDecoder().decode(base64);createFolder(outputDir);// 输出的PDF文件名String outputFilePath = outputDir+"/output.pdf";try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {// 将解码后的字节数组写入文件fos.write(decodedBytes);System.out.println("PDF文件已成功生成: " + outputFilePath);words = getWords(outputFilePath);} catch (Exception e) {e.printStackTrace();}deleteDirectory(outputDir);return words;}}
http://www.yayakq.cn/news/717754/

相关文章:

  • 如何用vs的c 做网站安阳网站建设商祺
  • 房地产行业网站开发搜索引擎网站盈利模式
  • 南京建站服务福建注册公司网上申请入口
  • 网站建设英文术语做wps的网站赚钱
  • 淡水网站建设公司温州网站关键词
  • 工信部备案系统网站蛋糕网站模版
  • 运城网站建设费用我要自学网视频教程
  • 做网站学什么专业网站上地图怎么做的
  • 青岛网站开发培训庆阳网站制作
  • 称多网站建设做公众号用什么网站
  • 站群网站内容陕西公共资源交易中心
  • 厅网站建设中标公告800多块做网站
  • 怎么建立一个博客网站吗苏中建设集团网站
  • 网站建设营销模板网站开发用到的框架
  • 创建一个自己的网站的步骤微擎做的网站好排名吗
  • 广州网站建设网络推广公司龙岩天宫山攻略
  • 西安专业做网站建国外教育网站模板
  • 建设交流网站微信平台开发
  • 企业网站设计能否以app制作教程培训
  • 免费的舆情网站入口在哪不用写代码做的网站
  • 网站建设中广告图片尺寸榆林电商网站建设
  • 苏州做网站的公司排名国外最大的素材网站
  • 网站建设公司推荐金石下拉网络wordpress图片数据库
  • 培训型网站建设网站用php做的吗
  • 家用电脑和宽带做网站网站主题有哪些
  • 自己做信息网站网站建设如何接单
  • 石家庄网站建设哪家好微信社群营销推广方案
  • 医院网站建设存在问题做网站时的注册权起到什么作用
  • 台州做网站网店推广目的
  • thinkphp怎么做网站宁阳房产网