Codename One POST pic到ocr.space

时间:2017-02-05 08:37:41

标签: json api ocr codenameone

我在将图片发布到ocr.space时遇到问题。他们的api支持文件,网址和base64images。我唯一不愿意接受的是网址。

发布文件时收到的错误是"它不是正确的文件"。

如何发布文件或base64image?这是我的代码:

public void pictureUploads(final Callback<String> resultURL) {
String picture = Capture.capturePhoto(1024, -1);
if(picture!=null){
    String filestack = "https://api.ocr.space/parse/image";
    MultipartRequest request = new MultipartRequest() {
       protected void readResponse(InputStream input) throws IOException  {
          JSONParser jp = new JSONParser();
          Map<String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
          System.out.println("" + result);              
          String url = (String)result.get("ParsedText");
          if(url == null) {

             resultURL.onError(null, null, 1, result.toString());
             return;
          }
          resultURL.onSucess(url);
       }
    };
    request.setUrl(filestack);

    try {
        request.setPost(true);
        request.addData("file", picture, "image/jpg"); 
        request.setContentType("image/jpeg");

        request.addArgument("apikey", "helloWorld");
        NetworkManager.getInstance().addToQueue(request);
    } catch(IOException err) {
    }
}

}

1 个答案:

答案 0 :(得分:2)

解决了,我需要在addData:request.setFilename(“file”,“ocr.jpeg”)之后添加这一行;