我想使用 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"}
不知道为什么......
关于java - 如何使用 Base 64 调用 ocr.space API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56759348/