Передача вывода функции Javascript в другую переменную - PullRequest
0 голосов
/ 27 декабря 2018

Я разрабатываю настольное приложение, в котором мне нужно распечатать изображение ниже image

Я передаю rgb изображение следующей javascript функции

 function floyd_steinberg(image) {
  alert("h");
  // var data;
  var imageData = image.data;
  var imageDataLength = imageData;
  var w = image.width;
  var lumR = [],
      lumG = [],
      lumB = [];

  var newPixel, err;

  for (var i = 0; i < 256; i++) {
    lumR[i] = i * 0.299;
    lumG[i] = i * 0.587;
    lumB[i] = i * 0.110;
  }

  // Greyscale luminance (sets r pixels to luminance of rgb)
  for (var i = 0; i <= imageDataLength; i += 4) {
    imageData[i] = Math.floor(lumR[imageData[i]] + lumG[imageData[i+1]] + lumB[imageData[i+2]]);
  }

  for (var currentPixel = 0; currentPixel <= imageDataLength; currentPixel += 4) {
    // threshold for determining current pixel's conversion to a black or white pixel
    newPixel = imageData[currentPixel] < 150 ? 0 : 255;
    err = Math.floor((imageData[currentPixel] - newPixel) / 23);
    imageData[currentPixel] = newPixel;
    imageData[currentPixel + 4         ] += err * 7;
    imageData[currentPixel + 4 * w - 4 ] += err * 3;
    imageData[currentPixel + 4 * w     ] += err * 5;
    imageData[currentPixel + 4 * w + 4 ] += err * 1;
    // Set g and b pixels equal to r (effectively greyscales the image fully)
    imageData[currentPixel + 1] = imageData[currentPixel + 2] = imageData[currentPixel];
  }
  image.data = imageData;
  console.log(image);

  return image;
}

Когда изображение передается вышеуказанной функции.Функция выводит изображение, и этот вывод должен быть сохранен в переменной.

Я попытался преобразовать монохромное изображение rgb, используя php, и сохранить выходное изображение в переменной, которая происходит успешно, ноЯ не могу сохранить в функции javascript.Ниже приведена функция php, которая берет входное изображение и сохраняет его в целевом файле

 function image2grf($filename='photo_url', $targetname = 'R:IMAGE.GRF')
{
  $info = getimagesize($filename);
  $im = imagecreatefrompng($filename);
  $width = $info[0]; // imagesx($im);
  $height = $info[1]; // imagesy($im);
  $depth = $info['bits'] ?: 1;
  $threshold = $depth > 1 ? 100 : 0;
  $hexString = '';
  $byteShift = 7;
  $currentByte = 0;
  // iterate over all image pixels
  for ($y = 0; $y < $height; $y++) {
    for ($x = 0; $x < $width; $x++) {
      $color = imagecolorat($im, $x, $y);
      // $color = imagetruecolortopalette($photo_url, true, 2);
      // compute gray value from RGB color
      if ($depth > 1) {
        $value = max($color >> 16, $color >> 8 & 255, $color & 255);
      } else {
        $value = $color;
      }
      // set (inverse) bit for the current pixel
      $currentByte |= (($value > $threshold ? 0 : 1) << $byteShift);
      $byteShift--;
      // 8 pixels filled one byte => append to output as hex
      if ($byteShift < 0) {
        $hexString .= sprintf('%02X', $currentByte);
        $currentByte = 0;
        $byteShift = 7;
      }
    }
    // append last byte at end of row
    if ($byteShift < 7) {
      $hexString .= sprintf('%02X', $currentByte);
      $currentByte = 0;
      $byteShift = 7;
    }
    $hexString .= PHP_EOL;
  }
  // compose ZPL ~DG command
  $totalBytes = ceil(($width * $height) / 8);
  $bytesPerRow = ceil($width / 8);
return sprintf('~DG%s,%05d,%03d,' . PHP_EOL, $targetname, $totalBytes, $bytesPerRow) . $hexString;
}
// Usage:
print "^XA N ^XZ" . PHP_EOL;
print image2grf($photo_url, 'R:SAMPLE.GRF');

В последней строке кода php image2grf() выходные данные этой функции сохраняются в R:SAMPLE.GRF

Пожалуйста, помогите мне решить эту проблему, потому что я новичок в этом javascript

...