我正在尝试将数组缓冲区js传递给wasm OpenCV,但有时在使用imdecode函数时会引发异常或某些时间为空白数组。简单HTML:
<input type='file' id accept='image/*' onchange='openFile(event)'>
Javascript代码
var openFile = function (e) {
const fileReader = new FileReader();
fileReader.onload = (event) => {
const uint8Arr = new Uint8Array(event.target.result);
passToWasm(uint8Arr);
};
fileReader.readAsArrayBuffer(e.target.files[0]);
};
function passToWasm(uint8ArrData) {
// copying the uint8ArrData to the heap
const numBytes = uint8ArrData.length * uint8ArrData.BYTES_PER_ELEMENT;
const dataPtr = Module._malloc(numBytes);
const dataOnHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, numBytes);
dataOnHeap.set(uint8ArrData);
// calling the Wasm function
const res = Module._image_input(dataOnHeap.byteOffset, uint8ArrData.length);
}
C ++代码:
extern "C"
{
int image_input(uint8_t* buffer, size_t nSize) //query image input
{
Mat raw_data = cv::Mat(1, nSize, CV_8UC1, buffer);
img_object = cv::imdecode(raw_data, cv::IMREAD_UNCHANGED);
cout << img_object << endl;
return 1
}
}
[请帮助我,我已经花了很多天来解决这个问题。我正在以下问题的帮助下尝试相同的操作。
How to pass image frames camera to a function in wasm (C++)?
malloc()
和HEAP8.set()
可用于实现该目标。 Surma's article描述了如何详细执行此操作。另一个示例是此decode()
函数,该函数从JavaScript ArrayBuffer设置Wasm中的值。