我正在使用 Microsoft Cognitive Computer Vision API(缩略图功能)。
我正在尝试使用 JavaScript,但我无法理解响应。
我的整个嵌入了 JS 代码的 HTML 文档如下:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<button id="btn">Click here</button>
<p id="response">
<script type="text/javascript">
$('#btn').click(function () {
$.ajax({
url: "https://api.projectoxford.ai/vision/v1.0/generateThumbnail?width=100&height=100&smartCropping=true",
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "382f5abd65f74494935027f65a41a4bc");
},
type: "POST",
data: '{"url": "https://oxfordportal.blob.core.windows.net/emotion/recognition1.jpg"}'
})
.done(function (response) {
$("#response").text(response);
})
.fail(function (error) {
$("#response").text(error);
});
});
</script>
</body>
</html>
我得到的响应似乎不是 JSON,它看起来像这样:
如何处理来自此 API 的响应,以便将图像作为 base 64 字符串获取,我可以将其设置为图像元素上的 src。
它最终会变成这样,但我不知道如何得到它<base64string>
。
<img src="data:image/png;base64,<base64string>">
我已经在https://dev.projectoxford.ai/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fb/console的 api 测试控制台中尝试了一切,它似乎工作正常。