【发布时间】:2018-05-15 06:37:02
【问题描述】:
我想将处理后的图像从 python 后端发送到 C# 前端。 所以我使用JPG编码器对其进行编码并发送。
当我收到它并在 C# 中使用 jpegdecoder 对其进行解码时,它会抛出异常。其中接收字节的大小等于编码后发送的字节。
谁能指导我如何解码这张图片并显示它。
Python 服务器端编码图像 JPG 中的代码
while True:
conn, addr = s.accept()
print ('connected to : ' + addr[0] +" :"+ str(addr[1]))
vidcap = cv2.VideoCapture(videoPath)
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
conn.setblocking(0)
for mm in range(0,total_frames ,1):
try:
dataClient = str(conn.recv(4096).decode('UTF-8'))
conn.send(str.encode(dataClient))
conn.close()
kk=2
break
except socket.error:
ret,img= vidcap.read()
image = cv2.resize(img, (640, 480))
#############################################
encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),100]
result,enData=cv2.imencode('.jpg',image,encode_param)
conn.send(enData)
#############################################
C# 客户端解码图像 JPG 中的代码
const int PORT_NO = 6666;
//const string SERVER_IP = "210.107.232.138"; // 210.107.232.138 127.0.0.1
string SERVER_IP = IpAddress.Text;
client = new TcpClient(SERVER_IP, PORT_NO);
nwStream = client.GetStream();
bytesToRead = new byte[client.ReceiveBufferSize];
bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
/////////////////////////////////////
MemoryStream ms = new MemoryStream(bytesRead);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); //line 129
BitmapSource bitmapSource = decoder.Frames[0];
/////////////////////////////////////
var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
src.BeginInit();
src.Source = bitmapSource;
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32;
src.EndInit();
//copy to bitmap
Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data);
pictureBox2.Image = bitmap;
【问题讨论】:
-
如果 bytesRead 小于 Python 代码发送的字节数怎么办?不能保证您将在单个接收事件中接收到完整的数据缓冲区。另外,你有什么理由不能只使用
Image.FromStream(...)方法吗? -
我也尝试过,但它也不起作用
-
你在 MemoryStream 中没有放任何东西。您只需使用
bytesRead确定的大小对其进行初始化。您可能想使用new MemoryStream(bytesToRead)? -
我检查两边的字节大小。这些都是一样的。
-
再次阅读我的评论。您的问题不在于接收(尽管正如约翰已经指出的那样,它有些缺陷)。你的 MemoryStream 是空的。你正在使用这个构造函数:msdn.microsoft.com/en-us/library/bx3c0489(v=vs.110).aspx,但你需要这个:msdn.microsoft.com/en-us/library/e55f3s5k(v=vs.110).aspx
标签: c# python sockets stream jpeg