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?
Asked
Active
Viewed 5,211 times
1 Answers
2
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"
orfetch().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.
-
For canvas, note that they don't seem to support recognition on transparent pngs : they do convert all transparent pixels to black, and doing so, they loose a lot of precision. – Kaiido Apr 07 '17 at 15:25
-
Thanks for your answer. I understood, but I wanna receive a base64 encoded image and convert in a blob to send in ocr cognitive service using nodejs. I tried do this, but I did not have knowledge to do this. – Rodolfo Oliveira Apr 08 '17 at 21:28
-
@RodolfoOliveira this is the last bullet of my answer, and has been answered many times on SO. So please, follow the link. – Kaiido Apr 08 '17 at 23:50
-
I saw this. But Blob is defined in DOM Api and NodeJS is not based in DOM Implementation, correct? How can i do this without Blob Object? – Rodolfo Oliveira Apr 10 '17 at 00:38
-
Yes it's correct ... but how could have we known that you are using node ? Nothing in your question states so, next time you've got a node question add the [node.js] tag, as it stands now, you are only talking about front-end javascript, which is on what my answer is based. So changing it now would invalidate my answer, which is... not great. My knowledge in node is limited but what you want is just [how to convert a dataURI to binary](http://stackoverflow.com/questions/11335460/how-do-i-parse-a-data-url-in-node) and [how to post this binary](http://stackoverflow.com/questions/25344879/) – Kaiido Apr 10 '17 at 01:06