尝试使用清晰的Node.js调整流图像的大小 [英] try to resize the stream image with sharp Node.js

查看:153
本文介绍了尝试使用清晰的Node.js调整流图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用清晰的功能调整从用户到服务器的输入Stream-image的宽度和高度,但是图像没有任何反应.它保持他的原始尺寸,我该如何使用清晰功能来缩小或放大图像?

I am trying to resize the width and height of input Stream-image from the user to the server with sharp function but nothing happens with the image. It keeps on his original size, How should I use the sharp function so that I can get the image smaller or bigger?

请帮助我

这是我的代码的样子:

'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,效果很好.

I have used Sharp in the below way in my case which works perfectly fine.

            sharp('stream')
            .png()
            .resize(100, 100)
            .toBuffer((err, buffer, info) => {
                if (err)
                    console.log(err);

                if (buffer) {
                    return buffer;
                }
            });

Sharp的 toFile()将输出保存在文件中,因此您可以指定文件名作为参数. toBuffer()将返回一个缓冲的对象. 希望对您有帮助!

Sharp's toFile() saves the output in a file, so you could give a file name as an argument. toBuffer() will return a buffered object. Hope it helps!

这篇关于尝试使用清晰的Node.js调整流图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆