0

我在 v8 中使用 How to return cv::Mat in nodejs addon作为参考编写了一个 node js 插件。一旦我将接收到的缓冲区写入节点 js 中的 png 文件,我仍然会得到一个损坏的图像。我的原始图像是 png,这是我的代码

节点插件代码(CPP):

void myAddOnFunction(const v8::FunctionCallbackInfo<v8::Value>& args)
{
Mat image = cv::imread(image_path);

//Do some processing and return an Opencv Mat    
Mat image_result = processFunction();

unsigned int image_size = image_result.total() * image_result.elemSize();
char* image_char = reinterpret_cast<char*>(image_crop.data);

//Nan::MaybeLocal<v8::Object> imageBuffer = Nan::CopyBuffer(image_char, image_size);            
v8::MaybeLocal<v8::Object> imageBuffer = node::Buffer::Copy(isolate, image_char, image_size);
args.GetReturnValue().Set(imageBuffer.ToLocalChecked());
}

节点js代码片段

let result = addon.myAddOnFunction("Image_Path");
const out_file = path.basename("Output.png");
fs.writeFileSync(out_file, new Buffer.from(result, "binary"));

我使用文件类型模块来检查返回的文件类型,这就是我得到的

{ext:'mp1',mime:'audio/mpeg'}

我哪里会出错?

4

0 回答 0