Mic*_*hal 2 javascript buffer node.js
我有包含位图信息的二进制 nodejs 缓冲区对象。如何从缓冲区制作图像并将其保存到文件?
编辑:
我尝试使用@herchu 所说的文件系统包,但如果我这样做:
let robot = require("robotjs")
let fs = require('fs')
let size = 200
let img = robot.screen.capture(0, 0, size, size)
let path = 'myfile.png'
let buffer = img.image
fs.open(path, 'w', function (err, fd) {
if (err) {
// Something wrong creating the file
}
fs.write(fd, buffer, 0, buffer.length, null, function (err) {
// Something wrong writing contents!
})
})
Run Code Online (Sandbox Code Playgroud)
我得到
小智 6
尽管@herchu 和@Jake 的解决方案有效,但它们非常慢(以我的经验为 10-15 秒)。
Jimp 支持将原始像素缓冲区转换为开箱即用的PNG,并且工作速度更快(亚秒级)。
const img = robot.screen.capture(0, 0, width, height).image;
new Jimp({data: img, width, height}, (err, image) => {
image.write(fileName);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11935 次 |
| 最近记录: |