Я нашел решение, довольно простое.
function captureFrame()
{
console.log("capture frame start");
console.log("cols = ", global_frame.cols, ", rows = ", global_frame.rows, ", type = ", global_frame.type(), ", steps = ", global_frame.step[0]);
global_cap.read(global_frame);
console.log("convert from rgba 2 rgb");
cv.cvtColor(global_frame, global_rgb_frame, cv.COLOR_RGBA2RGB);
HEAPU8.set(global_rgb_frame.data, global_buffer);
console.log("buffer values = ", buffer[0], ", ", buffer[1], ", ", buffer[2]);
console.log("global_rgb_frame.data values = ", global_rgb_frame.data[0], ", ", global_rgb_frame.data[1], ", ", global_rgb_frame.data[2]);
return global_buffer;
}
global_buffer - это куча, которую я запускаю при запуске камеры, чтобы уменьшить необходимость выделения памяти, я делаю ее как глобальную переменную, этот уродливый прием часто встречается, когда мне нужно общаться на разных языках.
try{
// Get a permission from user to use a camera.
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
global_camera.srcObject = stream;
let {width, height} = stream.getTracks()[0].getSettings();
global_height = height;
global_width = width;
console.log("width x height of stream = ", width, "," , height);
global_frame = new cv.Mat(height, width, cv.CV_8UC4);
global_rgb_frame = new cv.Mat(height, width, cv.CV_8UC3);
global_camera.setAttribute("width", width);
global_camera.setAttribute("height", height);
global_buffer = _malloc(global_rgb_frame.rows * global_rgb_frame.cols * 3);
global_camera.onloadedmetadata = function(e) {
global_camera.play();
//! [Open a camera stream]
global_cap = new cv.VideoCapture(global_camera); //*/
};
});
}catch(err){
console.log("err:", err, ", err messages:", err.message);
}
После этого зарегистрируйте функцию в файле cpp.
EM_JS(unsigned char*, capture_frame, (), {
return captureFrame();
})
Преобразование буфера к QImage и нарисуйте его на QLabel
auto *buffer = capture_frame();
auto const width = get_frame_width(); //access js global variable
auto const height = get_frame_height(); //access js global variable
qDebug()<<__func__<<"width x height = "<<width<<", "<<height;
qDebug()<<__func__<<"buffer values = "<<buffer[0]<<", "<<buffer[1]<<", "<<buffer[2];
cv::Mat frame(height, width, CV_8UC3, buffer);
QImage img(frame.data, frame.cols, frame.rows,
static_cast<int>(frame.step[0]), QImage::Format_RGB888);
ui->labelImage->setPixmap(QPixmap::fromImage(img.copy()));