通过网络发送图像 [英] Send an image over a network
问题描述
我在之前提出过有关通过网络发送结构化数据的问题 ,以及如何在服务器端处理这些数据。
I've asked a question before about sending structured data over network, and how to deal with those data on server side.
我真正想做的是通过网络从客户端发送一个图像(c ++ / Qt )到服务器端(python)。
What I'm actualy trying to do, is sending an image over a network from client side (c++/Qt) to server side (python).
两边都使用 OpenCV库
我发现这些OpenCV函数可以帮助我: cv :: imencode()
, cv :: imdecode()
and I've found these OpenCV functions that could help me I think: cv::imencode()
, cv::imdecode()
发送部分(客户端):
cv::Mat img = cv::imread("lena.png")
vector<uchar> buf;
cv::imencode( ".png", img, buf );
// rawImage: the data that are supposed to be sended and decoded with cv2.imdecode()
QByteArray rawImage = QByteArray( (const char *) buf.data(), buf.size() );
// construct paquet
QByteArray sep = QByteArray();
sep.append( "<break>" );
QByteArray paquet;
paquet.append( _token );
paquet.append( sep );
paquet.append( query );
paquet.append( sep );
paquet.append( QJsonDocument( jsonObject ).toJson( QJsonDocument::Compact ) );
paquet.append( sep );
paquet.append( rawImage );
write( paquet );
接收部分(服务器端)
# get the data here...
lines = data.split( '<break>' )
raw = lines[3]
img = cv2.imdecode( raw, cv2.CV_LOAD_IMAGE_COLOR )
# process img ...
发生此错误:
...
img = cv2.imdecode( raw, cv2.CV_LOAD_IMAGE_COLOR )
exceptions.TypeError: buf is not a numpy array, neither a scalar
我认为发送(或编码)这个图像的更好的方式。一些帮助?
I think it's not the better way to send (or encode) this image. some help ?
编辑:
我认为OpenCV方法在C ++和Python版本之间不兼容,图像不以相同的方式存储在存储器中。有人确认?
I think that the OpenCV fonctions are not compatible between the C++ and the Python version, and the images are not stored the same way in memory. Someone to confirm ?
推荐答案
您可以使用多部分请求来避免编码/解码,值对。在PHP中,使用多部分请求上传的图像可通过 $ _ FILES
变量访问。可以用 QHttpMultiPart
类实现。
You can use multi-part request to avoid encoding/decoding, it allows to upload image and key/value pairs simultaneously. In PHP, image uploaded with multi-part request will be accessible through $_FILES
variable. Can be implemented with QHttpMultiPart
class.
这篇关于通过网络发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!