您好,我正在尝试使用 Microsoft 的 OCR API,它期望内容类型应用程序/八位字节流和正文发布二进制文件。
我尝试将图像作为 Base64(二进制)发送,只是二进制,但是它不起作用。
有人知道这张图片需要如何发送吗?
文档链接 https://westus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fc
是的,您只需将其作为Blob https://developer.mozilla.org/en/docs/Web/API/Blob or a File https://developer.mozilla.org/en/docs/Web/API/File(几乎是相同的事情)。
使用 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,这实际上取决于您从哪里获取图像。
- 如果它来自
<input type="file">
,然后就可以这样发送了。
- 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())
.
- 如果你有画布,那么你可以使用它toBlob https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob method.
- 如果你只有一个 dataURI,那么检查this Q/A https://stackoverflow.com/questions/12168909/blob-from-dataurl.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)