如何使用javascript在azure文本识别API中发布图像而不是URL?
我正在使用此代码进行文本识别。它以URL作为输入 我希望上传一张图片而不是给出URL 请提供帮助。这很有效:请记住在API密钥和区域中加入sub 基本上,您需要添加一个file类型的输入,读取文件读取器的数组缓冲区,当您选择一个文件时,该缓冲区会做出反应,然后使用application/octet stream作为内容类型提交数据 HTML:如何使用javascript在azure文本识别API中发布图像而不是URL?,javascript,microsoft-cognitive,azure-cognitive-services,Javascript,Microsoft Cognitive,Azure Cognitive Services,我正在使用此代码进行文本识别。它以URL作为输入 我希望上传一张图片而不是给出URL 请提供帮助。这很有效:请记住在API密钥和区域中加入sub 基本上,您需要添加一个file类型的输入,读取文件读取器的数组缓冲区,当您选择一个文件时,该缓冲区会做出反应,然后使用application/octet stream作为内容类型提交数据 HTML: 唯一的区别是,您没有传入URL,而是选择了图像的二进制数据,并传入,将内容类型设置为application/octet stream或multipart/
唯一的区别是,您没有传入URL,而是选择了图像的二进制数据,并传入,将内容类型设置为application/octet stream或multipart/form data您知道如何在Java中实现吗?
<h1>Read handwritten image image:</h1>
Image to read:
<input type="file" id="inputImage" />
<br>
<br>
<div id="wrapper" style="width:1020px; display:table;">
<div id="jsonOutput" style="width:600px; display:table-cell;">
Response:
<br>
<br>
<textarea id="responseTextArea" class="UIInput" style="width:580px; height:400px;"></textarea>
</div>
</div>
document.querySelector('#inputImage').addEventListener('change', function() {
var reader = new FileReader();
reader.onload = function() {
var arrayBuffer = this.result,
array = new Uint8Array(arrayBuffer);
// Replace the subscriptionKey string value with your valid subscription key.
var subscriptionKey = "YOUR-KEY-HERE";
var uriBase = "https://YOUR-LOCATION-HERE.api.cognitive.microsoft.com/vision/v1.0/RecognizeText";
var params = {
"handwriting": "true",
};
$.ajax({
url: uriBase + "?" + $.param(params),
beforeSend: function(jqXHR) {
jqXHR.setRequestHeader("Content-Type", "application/octet-stream");
jqXHR.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
},
type: "POST",
processData: false,
data: arrayBuffer
})
.done(function(data, textStatus, jqXHR) {
// Show progress.
$("#responseTextArea").val("Handwritten text submitted. Waiting 10 seconds to retrieve the recognized text.");
setTimeout(function() {
// The "Operation-Location" in the response contains the URI to retrieve the recognized text.
var operationLocation = jqXHR.getResponseHeader("Operation-Location");
$.ajax({
url: operationLocation,
beforeSend: function(jqXHR) {
jqXHR.setRequestHeader("Content-Type", "application/json");
jqXHR.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
},
type: "GET",
})
.done(function(data) {
// Show formatted JSON on webpage.
$("#responseTextArea").val(JSON.stringify(data, null, 2));
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Display error message.
var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" : (jQuery.parseJSON(jqXHR.responseText).message) ?
jQuery.parseJSON(jqXHR.responseText).message : jQuery.parseJSON(jqXHR.responseText).error.message;
alert(errorString);
});
}, 10000);
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Put the JSON description into the text area.
$("#responseTextArea").val(JSON.stringify(jqXHR, null, 2));
// Display error message.
var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" : (jQuery.parseJSON(jqXHR.responseText).message) ?
jQuery.parseJSON(jqXHR.responseText).message : jQuery.parseJSON(jqXHR.responseText).error.message;
alert(errorString);
})
}
reader.readAsArrayBuffer(this.files[0]);
}, false);