如何在节点 js 中响应 api(计算机视觉 - 天蓝色的缩略图服务)的网页中显示图像
How to show image in web page which came response of api ( computer vision - thumbnil service in azure ) in node js
这是来自api
的回复
apim-request-id: a6ac620f-8484-4bdf-a9ed-d26080186769
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Mon, 24 Jun 2019 09:10:23 GMT
Content-Length: 49368
Content-Type: image/jpeg
这里,调用代码api
const express = require("express");
const app = express();
const http = require("http").Server(app).listen(8080);
const fs=require("fs");
const request = require('request');
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = 'key';
const uriBase =
'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';
const imageUrl =
'https://smworld.co.in/images/bg1.jpg';
// Request parameters.
const params = {
'width': '300',
'height': '300',
'smartCropping': 'true'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
我尝试在@Wiktor Zychla 评论中测试解决方案,但它只适用于 png
图片,不适用于 jpeg
。因此,我尝试给出将图像 url 嵌入静态 html 内容的解决方案。
这是我的示例代码,供您参考。
var request = require('request');
const subscriptionKey = 'key';
const uriBase = 'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';
const imageUrl = 'https://smworld.co.in/images/bg1.jpg';
// Request parameters.
const params = {
'width': '300',
'height': '300',
'smartCropping': 'true'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
const express = require('express')
const app = express()
app.get('/img', function(req, res) {
request.post(options).pipe(res)
})
// embed an image url `/img` into the html code
app.get('/', function (req, res) {
var html = '<h2>Hello world</h2></br><img src="/img"/>'
res.send(html)
})
app.listen(3000)
以上代码在我的本地环境中的结果如下运行
希望对您有所帮助。
这是来自api
的回复apim-request-id: a6ac620f-8484-4bdf-a9ed-d26080186769
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Mon, 24 Jun 2019 09:10:23 GMT
Content-Length: 49368
Content-Type: image/jpeg
这里,调用代码api
const express = require("express");
const app = express();
const http = require("http").Server(app).listen(8080);
const fs=require("fs");
const request = require('request');
// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = 'key';
const uriBase =
'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';
const imageUrl =
'https://smworld.co.in/images/bg1.jpg';
// Request parameters.
const params = {
'width': '300',
'height': '300',
'smartCropping': 'true'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
我尝试在@Wiktor Zychla 评论中测试解决方案,但它只适用于 png
图片,不适用于 jpeg
。因此,我尝试给出将图像 url 嵌入静态 html 内容的解决方案。
这是我的示例代码,供您参考。
var request = require('request');
const subscriptionKey = 'key';
const uriBase = 'https://centralindia.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail';
const imageUrl = 'https://smworld.co.in/images/bg1.jpg';
// Request parameters.
const params = {
'width': '300',
'height': '300',
'smartCropping': 'true'
};
const options = {
uri: uriBase,
qs: params,
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey
}
};
const express = require('express')
const app = express()
app.get('/img', function(req, res) {
request.post(options).pipe(res)
})
// embed an image url `/img` into the html code
app.get('/', function (req, res) {
var html = '<h2>Hello world</h2></br><img src="/img"/>'
res.send(html)
})
app.listen(3000)
以上代码在我的本地环境中的结果如下运行
希望对您有所帮助。