cv::Mat is not a buffer type or adaptable as such. If you have POD data, then you can "cast" it like the commenter says:
// don't do this:
buffer(&img, sizeof(img));
However, that's not safe:
static_assert(std::is_standard_layout_v<cv::Mat>);
static_assert(std::is_trivial_v<cv::Mat>);
The type is actually not bitwise copyable. But since there's a matrix, there is likely a contiguous region of data that is:
ws.write(net::buffer(img.data, img.total() * img.elemSize()));
This uses total()
and elemSize()
, links to the documentation.
Now, this will be enough if the receiving end already knows the dimensions. If not, send them first, e.g.:
uint32_t dimensions[] { htonl(image_witdh), htonl(image_height) };
std::vector<net::const_buffer> buffers {
net::buffer(dimensions),
net::buffer(img.data, img.total() * img.elemSize())
};
ws.write(buffers);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…