javascript - 尝试使用锐利的 Node.js 调整流图像的大小

标签 javascript node.js azure botframework chatbot

我正在尝试使用锐利功能调整从用户到服务器的输入流图像的宽度和高度,但图像没有任何反应。它保持原来的大小,我应该如何使用锐化功能,以便我可以使图像变小或变大?

请帮助我

这就是我的代码的样子:

'use strict';


const builder = require('botbuilder');
const restify = require('restify');
const utils = require('./utils.js');
const request = require('request');
const sharp = require('sharp');
const fs = require('fs');     
const resizeImage = require('resize-image');


// Create chat connector for communicating with the Bot Framework Service
const connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});

// Setup Restify Server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
    console.log(`${server.name} listening to ${server.url}`);
});

// Listen for messages from users
server.post('/api/messages', connector.listen());

const bot = new builder.UniversalBot(connector);

// default dialog

//resize the images

//Sends greeting message when the bot is first added to a conversation
bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                
         
                var reply = new builder.Message()
                    .address(message.address)
                    
                    .text('Hi, please send a screenshot for the error');
                    

                bot.send(reply);
            }
        });
    }
}
);

bot.dialog('/', function(session) {
    if(utils.hasImageAttachment(session)){
        //--others

        var stream = utils.getImageStreamFromMessage(session.message);
      ***//resize the image stream
      sharp('stream')
      .resize(100, 100)
      .toFile('stream', function(err) {
        // output.jpg is a 200 pixels wide and 200 pixels high image
        // containing a scaled and cropped version of input.jpg
      }); 
      //***
        const params = {
            'language': 'en',
            'detectOrientation': 'true',

        };
        const options = {
            uri: uriBase,
          qs: params,
            body: stream ,
          
            headers: {
                'Content-Type': 'application/octet-stream',
                'Ocp-Apim-Subscription-Key' : subscriptionKey
            }
        };

request.post(options, (error, response, body) => {
    if (error) {
      console.log('Error: ', error);
      return;
    }

 const obj =   JSON.parse(body);
 console.log(obj);

 
  //------------ get the texts from json as string
  if(obj.regions =="" ){
    
    session.send('OOOOPS I CANNOT READ ANYTHING IN THISE IMAGE :(');

}else{


let buf = ''
if(obj && obj.regions) {
obj.regions.forEach((a, b, c) => {
if(a && a.lines) {
a.lines.forEach((p, q, r) => {
if(p && p.words) {
p.words.forEach((x, y, z) => {
  

buf += ` ${x.text}  ` 



})
}
})
}
})
}
session.send(buf);
}

});
               

    } else {
        session.send('nothing');

        
    }
});

谢谢

最佳答案

根据函数Sharp的文档toFile() ,当没有提供回调时,此函数返回一个 promise 。

因此,在使用 toFile 函数时不应出现 I/O 阻塞,并继续运行以下代码,即代码片段中的 request.post 。届时,图像可能不会被修改。

您可以尝试使用 Promise 风格的代码流程,例如:

sharp('stream')
      .resize(100, 100)
      .toFile('stream')
      .then((err,info)=>{
         //do request post 
      })

或者将请求代码放在toFile()的回调函数中,例如:

sharp('stream')
      .resize(100, 100)
      .toFile('stream',function(err,info)=>{
       //do request post
      })

关于javascript - 尝试使用锐利的 Node.js 调整流图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983346/

相关文章:

Node.js 和 Protocol Buffer - 如何从帖子中解析 PB

azure - 从 AWS 发布到 Azure EventHub

javascript - 有没有轻量级的 JavaScript 日期选择器?

javascript - detach()、hide() 和 remove() 之间的区别 - jQuery

javascript - 刷新后express-flash消息为空

node.js - 使用 Windows 10 使用 nodemon 进行 Docker 热重载 Node 应用程序

javascript - Node js应用程序无法调用未定义的方法

javascript - Angular JS Express JS文件上传抛出错误,未定义不是函数

c# - 将内容流式传输为 Azure 函数输出

azure - 使用 VSTS 和 Azure 时正确管理应用程序设置