Javascript 将缓冲区作为不带空字节的Uint8Array传递

Javascript 将缓冲区作为不带空字节的Uint8Array传递,javascript,node.js,tesseract,Javascript,Node.js,Tesseract,我试图通过缓冲区将图像数据从图像对象传递到Tesserract(ocr库): image.getBufferAsync('image/png')。然后((buffer)=>{ //这里的缓冲区是{console.log('result',result.text);}); }); 它从Teserract抛出一个错误,表示它需要Uint8Array而不是buffer TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a s

我试图通过缓冲区将图像数据从图像对象传递到Tesserract(ocr库):

image.getBufferAsync('image/png')。然后((buffer)=>{
//这里的缓冲区是{console.log('result',result.text);});
});
它从Teserract抛出一个错误,表示它需要Uint8Array而不是buffer

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer 89 50 4e 47...
但我还有一个错误:

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received Uint8Array [
  137,
  80,
  ...
哪里有错误



如果我将图像文件保存到光盘上,然后通过Teserract读取其路径,它会工作,因此问题不应该是图像。

文档中指出,在Node JS中,
img
参数应该是本地图像的路径

在浏览器上,图像可以是:

  • img、视频或画布元素
  • 文件对象(来自文件)
  • 指向可访问映像的路径或URL
在Node.js中,可以创建图像

  • 指向本地映像的路径


这意味着库希望自己读取文件,而不是得到一个字节流进行分析。

下面的内容对我很有用。我想我们可能会使用不同版本的tesseract(3.05版),但不要认为它变化太大

    var Jimp = require('jimp');
    var Tesseract = require('tesseract.js');
    var file = 'YourFile.png';  // Or .jpg etc...

    Jimp.read(file, async (err, image) => {
        if (err) throw err;

        //Do your Jimp stuff here to 'image' then...

        const buffer = await image.getBufferAsync(Jimp.AUTO);

        //Above line creates a buffer using Jimp.AUTO (the  
        //original file format from your variable 'file')

        Tesseract.recognize(buffer,'eng')
            .then(data => {
             console.log(data)
            })
     })
    var Jimp = require('jimp');
    var Tesseract = require('tesseract.js');
    var file = 'YourFile.png';  // Or .jpg etc...

    Jimp.read(file, async (err, image) => {
        if (err) throw err;

        //Do your Jimp stuff here to 'image' then...

        const buffer = await image.getBufferAsync(Jimp.AUTO);

        //Above line creates a buffer using Jimp.AUTO (the  
        //original file format from your variable 'file')

        Tesseract.recognize(buffer,'eng')
            .then(data => {
             console.log(data)
            })
     })