java - 如何使用 Base 64 调用 ocr.space API

标签 java java-8 base64

我想使用 java 调用 ocr.space( https://ocr.space/ocrapi ) OCR API(基于 64 位)。

我使用了以下代码,但输出出现错误。

我使用过的代码...

try {
        URL obj = new URL("https://api.ocr.space/parse/image"); // OCR API Endpoints

        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        con.setRequestProperty("apikey", "******");

        con.setDoOutput(true);
        File file = new File("C:\\App_development\\OCRIMG.JPG");
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        InputStream finput = new FileInputStream(file);
        byte[] imageBytes = new byte[(int)file.length()];
        finput.read(imageBytes, 0, imageBytes.length);
        finput.close();
        String imageStr = Base64.encodeBase64String(imageBytes);
        wr.writeBytes(imageStr);
        wr.flush();
        wr.close();
        int responseCode = con.getResponseCode();


        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
    }
    catch(Exception exx)
    {
        exx.printStackTrace();
    }

我遇到异常

Response Code : 200
{"OCRExitCode":0,"IsErroredOnProcessing":false,"ErrorMessage":["Parameter name '/9j/……..' is invalid. Valid parameters: apikey,url,language,isoverlayrequired,base64image,iscreatesearchablepdf,issearchablepdfhidetextlayer,filetype,addressparsing,scale,detectorientation,istable,ocrengine,detectcheckbox,checkboxtemplate","Please check if you need to URL encode the URL passed in request parameters."],"ProcessingTimeInMilliseconds":"4"}

任何帮助都将得到高度重视......。

最佳答案

也许你可以改变

String imageStr = Base64.encodeBase64String(imageBytes);

 String imageStr ="&base64image="+ Base64.encodeBase64String(imageBytes)+":<JPG>";

但我遇见了

{"OCRExitCode":99,"IsErroredOnProcessing":true,"ErrorMessage":["Some internal processing error occurred. Please give us some time, we are looking into this!"],"ErrorDetails":"","ProcessingTimeInMilliseconds":"0"}

不知道为什么......

也许你可以引用这个: https://github.com/bsuhas/OCRTextRecognitionAndroidApp/blob/be7bb24a0e880cf174de9f16047fcb1b8c7447c6/app/src/main/java/com/ocrtextrecognitionapp/OCRAsyncTask.java

关于java - 如何使用 Base 64 调用 ocr.space API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56759348/

相关文章:

java - 无法将 base64 编码的字符串映射到 JAXRS 中的 Restful 实现

image - 使用Worklight适配器从derby检索存储为CLOB的base64图像

java - 无法加载ApplicationContext

确认脚本中的 Javascript IIF

java - 模拟蓝牙配件的简单应用程序

java - 这是在单线程还是多线程上运行?

java - 具有多个前导和尾随 1 和 0 的字符串的正则表达式模式

java - Lambda表达式和方法重载的疑惑

java - 流已被操作或关闭 - Java 8

c# - C#和Java的Base64区别