How to send image using application/octet-stream in OCR Cognitive

馋奶兔 提交于 2020-01-15 03:05:32

问题


Hello I'm trying to use OCR API From Microsoft and It expect Content-type application/octet-stream and body post a binary. I tried send image as Base64(binary), just binary, however It didn't work. Someone knows how this image needs be sended?

Link to documentation


回答1:


Yes, you can simply send it as a Blob or a File (which are almost the same things).

Example code using the XMLHttpRequest API :

var xhr = new XMLHttpRequest();
xhr.onload = do_something_with_this_JSON;
xhr.open('POST', 'https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr');
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("Ocp-Apim-Subscription-Key", YOUR_KEY);
xhr.send(blob);

Now on how to get a Blob, this really depends on where you get your image from.

  • if it comes from an <input type="file">, then you can send it like that.
  • if it comes from a request (then why don't you send the url as application/JSON?) you can request the response to be a blob (xhr.responseType = "blob" or fetch().then(resp => resp.blob()).
  • if you've got a canvas, then you can use its toBlob method.
  • if you only have a dataURI, then check this Q/A.


来源:https://stackoverflow.com/questions/43277553/how-to-send-image-using-application-octet-stream-in-ocr-cognitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!