{"id":294696,"date":"2024-05-20T15:16:56","date_gmt":"2024-05-20T07:16:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/294696.html"},"modified":"2024-05-20T15:17:11","modified_gmt":"2024-05-20T07:17:11","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e4%b8%ad%e8%b0%83%e7%94%a8paddleocr","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/294696.html","title":{"rendered":"\u5982\u4f55\u5728java\u4e2d\u8c03\u7528paddleocr"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27141852\/e15065fb-183a-4928-94c7-f348108cad0a.webp\" alt=\"\u5982\u4f55\u5728java\u4e2d\u8c03\u7528paddleocr\" \/><\/p>\n<p><p>\u8c03\u7528PaddleOCR\u5728Java\u4e2d\u8fdb\u884c\u6587\u672c\u8bc6\u522b\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<strong>\u5b89\u88c5\u4e0e\u51c6\u5907\u3001Java\u8c03\u7528Python\u811a\u672c\u3001\u5904\u7406OCR\u7ed3\u679c<\/strong>\u3002\u9996\u5148\uff0c\u9700\u8981\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5PaddleOCR\u7684Python\u73af\u5883\u4ee5\u53caJava\u5f00\u53d1\u73af\u5883\u3002\u7531\u4e8ePaddleOCR\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u7684\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Java\u4e2d\u7684<code>Runtime<\/code>\u6216<code>ProcessBuilder<\/code>\u7c7b\u6765\u8fd0\u884cPython\u811a\u672c\u3002\u7136\u540e\u6355\u83b7\u811a\u672c\u7684\u8f93\u51fa\u5e76\u5728Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5bf9\u4e8e\u6838\u5fc3\u6b65\u9aa4<strong>Java\u8c03\u7528Python\u811a\u672c<\/strong>\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5728Java\u4e2d\uff0c\u901a\u8fc7<code>ProcessBuilder<\/code>\u7c7b\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u5916\u90e8\u8fdb\u7a0b\u6765\u8fd0\u884c\u547d\u4ee4\u6216\u811a\u672c\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\uff0c\u5b83\u4f7f\u7528PaddleOCR\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u5e76\u8f93\u51fa\u8bc6\u522b\u7ed3\u679c\u3002\u7136\u540e\u5728Java\u4e2d\uff0c\u5229\u7528<code>ProcessBuilder<\/code>\u8c03\u7528\u8be5\u811a\u672c\u5e76\u4f20\u5165\u5fc5\u8981\u7684\u53c2\u6570\u4f8b\u5982\u56fe\u50cf\u8def\u5f84\u3002\u4e00\u65e6\u8fc7\u7a0b\u542f\u52a8\uff0c\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u51fa\u6d41\u6765\u8bfb\u53d6OCR\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u51c6\u5907<\/h3>\n<\/p>\n<p><h4>\u5b89\u88c5PaddleOCR\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Java\u4e2d\u8c03\u7528PaddleOCR\uff0c\u9996\u5148\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u6709Python\u4ee5\u53caPaddleOCR\u6240\u4f9d\u8d56\u7684\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5668pip\u6765\u5b89\u88c5PaddleOCR\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install paddlepaddle paddleocr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u5b89\u88c5\u8fc7\u7a0b\u6ca1\u6709\u9519\u8bef\uff0c\u5e76\u901a\u8fc7\u7b80\u5355\u7684Python\u811a\u672c\u6d4b\u8bd5PaddleOCR\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Java\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Java\u7684\u8fd0\u884c\u73af\u5883\uff08JRE\uff09\u548c\u5f00\u53d1\u73af\u5883\uff08JDK\uff09\u3002\u4f60\u53ef\u4ee5\u4eceOracle\u7684\u5b98\u65b9\u7f51\u7ad9\u6216\u5f00\u653eJDK\u7f51\u7ad9\u4e0b\u8f7d\u76f8\u5e94\u7684\u5b89\u88c5\u5305\u3002\u4f7f\u7528\u547d\u4ee4<code>java -version<\/code>\u68c0\u67e5Java\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Java\u8c03\u7528Python\u811a\u672c<\/h3>\n<\/p>\n<p><h4>\u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f7f\u7528PaddleOCR\u8fdb\u884cOCR\u8bc6\u522b\u3002\u811a\u672c\u63a5\u53d7\u547d\u4ee4\u884c\u53c2\u6570\u4f5c\u4e3a\u56fe\u7247\u8def\u5f84\uff0c\u5e76\u8f93\u51fa\u8bc6\u522b\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from paddleocr import PaddleOCR<\/p>\n<p>import sys<\/p>\n<p>def run_ocr(image_path):<\/p>\n<p>    ocr = PaddleOCR()<\/p>\n<p>    result = ocr.ocr(image_path)<\/p>\n<p>    for line in result:<\/p>\n<p>        print(&quot; &quot;.join([word[1][0] for word in line]))<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    image_path = sys.argv[1]<\/p>\n<p>    run_ocr(image_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5728Java\u4e2d\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u4f7f\u7528<code>ProcessBuilder<\/code>\u8c03\u7528\u4e0a\u8ff0\u7684Python\u811a\u672c\u5e76\u4f20\u5165\u56fe\u7247\u8def\u5f84\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import java.io.BufferedReader;<\/p>\n<p>import java.io.InputStreamReader;<\/p>\n<p>public class PaddleOCRCaller {<\/p>\n<p>    public static void callPaddleOCR(String imagePath) {<\/p>\n<p>        try {<\/p>\n<p>            ProcessBuilder pb = new ProcessBuilder(&quot;python&quot;, &quot;path_to_your_script.py&quot;, imagePath);<\/p>\n<p>            Process process = pb.start();<\/p>\n<p>            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));<\/p>\n<p>            String line;<\/p>\n<p>            while ((line = reader.readLine()) != null) {<\/p>\n<p>                System.out.println(line); \/\/ \u8f93\u51fa\u8bc6\u522b\u7684\u6587\u672c<\/p>\n<p>            }<\/p>\n<p>            int exitCode = process.waitFor();<\/p>\n<p>            if (exitCode != 0) {<\/p>\n<p>                \/\/ \u9519\u8bef\u5904\u7406<\/p>\n<p>                System.err.println(&quot;\u6267\u884c\u51fa\u9519!&quot;);<\/p>\n<p>            }<\/p>\n<p>        } catch (Exception e) {<\/p>\n<p>            e.printStackTrace();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        callPaddleOCR(&quot;path_to_your_image.jpg&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406OCR\u7ed3\u679c<\/h3>\n<\/p>\n<p><h4>\u6355\u83b7\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0Java\u4ee3\u7801\u4e2d\uff0c\u8bc6\u522b\u7ed3\u679c\u662f\u901a\u8fc7\u6807\u51c6\u8f93\u51fa\u6253\u5370\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u8fd9\u4e9b\u8f93\u51fa\u505a\u8fdb\u4e00\u6b65\u7684\u5904\u7406\uff0c\u6bd4\u5982\u89e3\u6790\u6570\u636e\u3001\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u6216\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u5904\u7406OCR\u8fd4\u56de\u7684\u6570\u636e\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fc7\u6ee4\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\uff0c\u6216\u8005\u53ea\u4fdd\u7559\u6570\u5b57\u7b49\u3002\u5728Java\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e9b\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import java.util.regex.Matcher;<\/p>\n<p>import java.util.regex.Pattern;<\/p>\n<p>public class OCRResultProcessor {<\/p>\n<p>    public static String extractNumbers(String text) {<\/p>\n<p>        Pattern pattern = Pattern.compile(&quot;\\\\d+&quot;);<\/p>\n<p>        Matcher matcher = pattern.matcher(text);<\/p>\n<p>        StringBuilder result = new StringBuilder();<\/p>\n<p>        while (matcher.find()) {<\/p>\n<p>            result.append(matcher.group());<\/p>\n<p>        }<\/p>\n<p>        return result.toString();<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ed3\u679c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u5904\u7406\u597d\u7684\u6570\u636e\u5e94\u7528\u5230\u5b9e\u9645\u7684\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c\u4f8b\u5982\u5236\u4f5c\u641c\u7d22\u5f15\u64ce\u7d22\u5f15\u3001\u6570\u636e\u5206\u6790\u7b49\u3002\u4efb\u4f55\u9700\u8981\u81ea\u7136\u8bed\u8a00\u6587\u672c\u5904\u7406\u7684\u9886\u57df\u90fd\u53ef\u4ee5\u4eceOCR\u6280\u672f\u4e2d\u53d7\u76ca\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Java\u4e2d\u6210\u529f\u8c03\u7528PaddleOCR\u8fdb\u884c\u56fe\u50cf\u4e2d\u6587\u5b57\u7684\u8bc6\u522b\u548c\u5904\u7406\u3002\u8fd9\u4e3a\u96c6\u6210\u590d\u6742\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u5230\u4f20\u7edf\u7684Java\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ef\u884c\u6027\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6211\u8be5\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528PaddleOCR\u5e93\u8fdb\u884c\u6587\u672c\u8bc6\u522b\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\u8c03\u7528PaddleOCR\u53ef\u4ee5\u901a\u8fc7Java\u7684JNI\uff08Java Native Interface\uff09\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e0b\u8f7d\u5e76\u7f16\u8bd1PaddleOCR\u5e93\u7684Java\u7248\u672c\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06\u7f16\u8bd1\u540e\u7684\u5e93\u6587\u4ef6\u52a0\u8f7d\u5230Java\u7a0b\u5e8f\u4e2d\uff0c\u5e76\u901a\u8fc7JNI\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u6765\u8fdb\u884c\u6587\u672c\u8bc6\u522b\u3002<\/p>\n<p><strong>2. \u6709\u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u5728Java\u4e2d\u8c03\u7528PaddleOCR\uff1f<\/strong><\/p>\n<p>\u5f53\u7136\u6709\uff01\u4f60\u53ef\u4ee5\u5728PaddleOCR\u7684\u5b98\u65b9GitHub\u9875\u9762\u627e\u5230\u4e00\u4e9b\u4f7f\u7528Java\u8c03\u7528PaddleOCR\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8fd9\u4e9b\u4ee3\u7801\u5c06\u6307\u5bfc\u4f60\u5982\u4f55\u51c6\u5907\u8f93\u5165\u56fe\u50cf\u3001\u8c03\u7528OCR\u51fd\u6570\u4ee5\u53ca\u83b7\u53d6\u8bc6\u522b\u7ed3\u679c\u3002\u540c\u65f6\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728PaddleOCR\u7684\u6587\u6863\u4e2d\u627e\u5230\u66f4\u591a\u5173\u4e8eJava API\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>3. \u6211\u8be5\u5982\u4f55\u5904\u7406Java\u4e2d\u8c03\u7528PaddleOCR\u65f6\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Java\u8c03\u7528PaddleOCR\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u786e\u4fdd\u4f60\u7684Java\u7248\u672c\u4e0ePaddleOCR\u7684\u7248\u672c\u517c\u5bb9\u3002\u53e6\u5916\uff0c\u4f60\u8fd8\u9700\u8981\u6ce8\u610f\u5185\u5b58\u7ba1\u7406\uff0c\u56e0\u4e3a\u56fe\u50cf\u8bc6\u522b\u53ef\u80fd\u4f1a\u5360\u7528\u8f83\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u9047\u5230\u4efb\u4f55\u9519\u8bef\u6216\u5f02\u5e38\uff0c\u53ef\u4ee5\u67e5\u9605PaddleOCR\u7684\u793e\u533a\u8bba\u575b\u6216\u53d1\u8d77\u4e00\u4e2a\u95ee\u9898\u6765\u83b7\u53d6\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8c03\u7528PaddleOCR\u5728Java\u4e2d\u8fdb\u884c\u6587\u672c\u8bc6\u522b\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u4e0e\u51c6\u5907\u3001Java\u8c03\u7528Python\u811a\u672c\u3001\u5904\u7406OC [&hellip;]","protected":false},"author":3,"featured_media":294714,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294696"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=294696"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294696\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/294714"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=294696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=294696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=294696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}