1

我正在使用以下函数代码来获取应用程序的图标,我通过所有函数调用获取相同的 base64 图像数据。

 iconExtractor = require('icon-extractor');
   function get_icon(appname,path) 
   {
   iconExtractor.getIcon(appname,path);
   iconExtractor.emitter.once('icon', function(data){
   console.log('Here is my context: ' + data.Context);
   console.log('Here is the path it was for: ' + data.Path);
   console.log('Here is the base64 image: ' + data.Base64ImageData);
   });
   }
4

1 回答 1

0

一个小时前,我也遇到了与图标提取器包相同的问题。我的第一个直觉也是检查事件发射器。但是,在检查了源代码后,我发现它与发射器无关。在源代码中,图标提取器不会清除每次调用中堆积的缓冲区,而是多次调用发射器。

2 年后您可能不再需要它,但为了避免将来出现问题,我将发布代码的固定版本。

var EventEmitter = require('events');
var fs = require('fs');
var child_process = require('child_process');
var _ = require('lodash');
var os = require('os');
var path = require('path');

var emitter = new EventEmitter();

function IconExtractor(){

  var self = this;
  var iconDataBuffer = "";

  this.emitter = new EventEmitter();
  this.iconProcess = child_process.spawn(getPlatformIconProcess(),['-x']);

  this.getIcon = function(context, path){
    var json = JSON.stringify({context: context, path: path}) + "\n";
    self.iconProcess.stdin.write(json);
  }

  this.iconProcess.stdout.on('data', function(data){

    var str = (new Buffer(data, 'utf8')).toString('utf8');

    iconDataBuffer += str;

    //Bail if we don't have a complete string to parse yet.
    if (!_.endsWith(str, '\n')){
      return;
    }

    //We might get more than one in the return, so we need to split that too.
    _.each(iconDataBuffer.split('\n'), function(buf){

      if(!buf || buf.length == 0){
        return;
      }

      try{
        self.emitter.emit('icon', JSON.parse(buf));
      } catch(ex){
        self.emitter.emit('error', ex);
      }

    });

    iconDataBuffer = "";
  });

  this.iconProcess.on('error', function(err){
    self.emitter.emit('error', err.toString());
  });

  this.iconProcess.stderr.on('data', function(err){
    self.emitter.emit('error', err.toString());
  });

  function getPlatformIconProcess(){
    if(os.type() == 'Windows_NT'){
      return path.join(__dirname,'/bin/IconExtractor.exe');
      //Do stuff here to get the icon that doesn't have the shortcut thing on it
    } else {
      throw('This platform (' + os.type() + ') is unsupported =(');
    }
  }

}

module.exports = new IconExtractor();

将图标提取器模块的 bin 文件夹复制到您可以到达的地方。使用此代码创建一个 js 文件。所以它会像 /YourFolder/bin/ 和 /YourFolder/copiedFilename.js 这样使用它

var iconExtractor = require('./YourFolderPath/copiedFilename');

我希望这会有所帮助。我本来打算把它推送到 Github 仓库,但很明显他不再维护这个模块了。

于 2019-05-04T22:31:51.827 回答