我正在使用以下功能代码来获取应用程序的图标,所有功能调用都获得相同的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);
   });
   }

最佳答案

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

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

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仓库中,但很显然他不再维护该模块了。

10-02 07:50