node.js - 如何在node js中的api(计算机视觉-azure中的缩略图服务)响应的网页中显示图像

标签 node.js image azure api azure-cognitive-services

这是来自 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)

上述代码在我的本地环境上运行的结果如下。

enter image description here

希望有帮助。

关于node.js - 如何在node js中的api(计算机视觉-azure中的缩略图服务)响应的网页中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735208/

相关文章:

Azure辅助角色装配位置

javascript - Jade Minified Javascript 未解析为文本 block

javascript - Jest 比 mocha 慢 40 倍

image - 有没有办法让 iPhone 应用程序只让用户下载他们的设备分辨率所需的图形?

android - 如何在上传到服务器之前减小图像文件大小

image - 使用 knockoutjs 在图像上显示工具提示悬停

javascript - Mongoose populate - Node.JS 创建多个对象

node.js - pm2集群模式不输出日志

azure - 使用 python 缓慢上传到 azure blob 存储

azure - 了解是否以及何时获得管理员同意