如何在OCR认知中使用应用程序/八位位组流发送图像 [英] How to send image using application/octet-stream in OCR Cognitive
本文介绍了如何在OCR认知中使用应用程序/八位位组流发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我正在尝试使用Microsoft的OCR API,它希望Content-type application/octet-stream和body发布一个二进制文件. 我尝试将图像发送为Base64(binary),只是二进制,但是没有用. 有人知道该图像如何发送吗?
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?
推荐答案
是的,您只需将其作为 Blob
或 File
(几乎是一样的东西).
Yes, you can simply send it as a Blob
or a File
(which are almost the same things).
使用XMLHttpRequest API的示例代码:
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);
现在介绍如何获取Blob,这实际上取决于您从何处获取图像.
Now on how to get a Blob, this really depends on where you get your image from.
- 如果它来自
<input type="file">
,则可以这样发送. - 如果它来自请求(那么为什么不将URL作为
application/JSON
发送?),则可以请求响应为Blob(xhr.responseType = "blob"
或fetch().then(resp => resp.blob())
). - 如果您有画布,则可以使用其此问题/答案.
- 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.
这篇关于如何在OCR认知中使用应用程序/八位位组流发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文