Hi,
I would like to use TCPIP or UDP to send a JPEG file to the computer. I would like to grab the data in memory and display it. using client/server communications. Can I do that. Or do I have to write to the hard drive as a file then to imread to display the image.
I am very inexperienced with OpenCV. I would like to do this in C++.
Thank You,
Gary
Yes you can do that.
chatGPT gives sample code
Sender (server)
#include <opencv2/opencv.hpp>
#include <asio.hpp> // single‑header Asio stand‑alone
using asio::ip::tcp;
int main() {
cv::Mat img = cv::imread("frame.jpg", cv::IMREAD_COLOR);
std::vector<uchar> buf;
cv::imencode(".jpg", img, buf);
asio::io_context io;
tcp::acceptor acc(io, tcp::endpoint(tcp::v4(), 5000));
tcp::socket sock(io);
acc.accept(sock);
// send size prefix (uint32_t, network byte order) then payload
uint32_t n = htonl(static_cast<uint32_t>(buf.size()));
asio::write(sock, asio::buffer(&n, sizeof n));
asio::write(sock, asio::buffer(buf));
}
Receiver (client)
#include <opencv2/opencv.hpp>
#include <asio.hpp>
using asio::ip::tcp;
int main() {
asio::io_context io;
tcp::socket sock(io);
sock.connect({asio::ip::make_address("127.0.0.1"), 5000});
uint32_t n_net; // read size prefix
asio::read(sock, asio::buffer(&n_net, sizeof n_net));
uint32_t n = ntohl(n_net);
std::vector<uchar> buf(n);
asio::read(sock, asio::buffer(buf)); // read JPEG bytes
cv::Mat img = cv::imdecode(buf, cv::IMREAD_COLOR);
cv::imshow("JPEG from RAM", img);
cv::waitKey();
}
if the source is a file, that (decoding and encoding) can be reduced to simply reading the file into the buffer, without imread and imencode.