簡體   English   中英

如何在 NodeJS 中使用 fs 打開圖像文件?

[英]How can I open an Image file using fs in NodeJS?

在我當前的代碼中,它只能讀取一個文本文件,如何制作一個使用照片應用程序(Windows)打開的圖像(base64)文件? 有沒有機會這樣做? 如果不可能,請告訴我!

const fs = require('fs')

fs.readFile('./Test/a.txt', 'utf8' , (err, data) => {
    if (err) {
      console.error(err)
      return
    }
    console.log(data)
    return
})

做這樣的事情:

const cp = require('child_process');
const c = cp.spawn('bash');  // 1

const imageFilePath = '/aaa/bbb/ccc'

c.stdin.end(` 
   program_that_opens_images "${imageFilePath}"
`);  // 2

c.stdout.pipe(process.stdout);  // 3
c.stderr.pipe(process.stderr);

c.once('exit', exitCode => {    // 4
   // child process has exited

});

它能做什么:

  1. 生成一個 bash 子進程(如果需要,請使用shzsh
  2. 寫入 bash 標准輸入,(輸入命令運行)
  3. 將 stdio 從子級傳輸到父級
  4. 捕獲孩子的退出代碼

另一種可能的解決方案是這樣的:

const cp = require('child_process');

const imageFilePath = '/aaa/bbb/ccc'

const c = cp.spawn('program_that_opens_images',[
 `"${imageFilePath}"`
]);  // 1


c.stdout.pipe(process.stdout);  // 3
c.stderr.pipe(process.stderr);

c.once('exit', exitCode => {    // 4
   // child process has exited

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM