Javascript如何将缓冲区中的图像数据二进制化

时间:2019-05-05 13:02:01

标签: javascript electron buffer

我正在创建一个Electron应用程序,并且已经以以下形式缓冲了图像中的数据:

[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 2, 88, 0, 0, 0, 120, 8, 2, 0, 0, 0, 161, 55, 27, 148, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 18, 0, 0, 11, 18, 1, 210, 221, 126, 252, 0, 0, 32, 0, 73, 68, 65, 84, 120, 218, 236, 189, 7, 152, 84, 69, 214, 255, 191, 207, 255, 217, 223, 238, 42, 97, 102, 58, 231, 112, 67, 231, 137, 12, 57, 231, 201, 211, 211, 211, 57, 119, 79, 102, 72, 2, …]

我需要转换为黑白图像,但是每当通过下面的二值化函数运行它时,都会出现类似TypeError: Cannot read property 'mime' of null的错误。

我正在使用以下代码将其转换为黑白:

function binarize(data) {
  var RED_INTENCITY_COEF = 0.2126;
  var GREEN_INTENCITY_COEF = 0.7152;
  var BLUE_INTENCITY_COEF = 0.0722;

  for(var i = 0; i < data.length; i += 4) {
    var brightness = RED_INTENCITY_COEF * data[i] + GREEN_INTENCITY_COEF * data[i + 1] + BLUE_INTENCITY_COEF * data[i + 2];
    // red
    data[i] = brightness;
    // green
    data[i + 1] = brightness;
    // blue
    data[i + 2] = brightness;
  }
  return data;
}

data = binarize(data)

图像仅在缓冲区中,不需要在任何地方显示。图片是从png文件中提取的。

0 个答案:

没有答案