使用命名管道将图像帧从 C++ 服务器发送到 C# 客户端
Send Image Frames from C++ Server to C# Client using Named Pipe
我是 IPC
的菜鸟。我正在尝试将图像帧从我的 C++ Server
发送到 C# Client
。我已经开始学习它并制作了一个小的 Client
和 Server
,我的 C++ Server
在其中发送 Hello
。我看到了一个相关的问题,有人告诉我先将 Image
转换为 Byte Array
,然后以与 Hello Message
相同的方式发送,但我无法做到这一点。
我的基本客户端服务器代码
C++代码:
Mat image = imread("IMG_0_10_34_45_2018_1.bmp");
uchar buffer[500][500];
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
buffer[i][j] = image.at<unsigned char>(i, j);
}
}
cout << "Server Creating Pipe\n";
HANDLE hPipe = ::CreateNamedPipe(_T("\\.\pipe\HyperPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
0,
NULL);
cout << "Server Created Succesfully";
ConnectNamedPipe(hPipe, NULL);
cout << "Sending Message to Client";
DWORD bytesWritten = 0;
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
CloseHandle(hPipe);
return 0;
和C#代码:
static void Main(string[] args)
{
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
{
System.Console.WriteLine("Message from Server: " + rdr.ReadToEnd());
}
Console.ReadKey();
}
我还注意到,在我的 C++ Server
中,我必须更改 PIPE_TYPE to BYTE
和 READMODE to BYTE
。我将 OpenCV library
用于 Image Processing
,因此我可以轻松地使 Byte Array
没有问题。
所以,任何人都可以告诉我如何将 Byte Array
从 C++
发送到 C#
。
或者如果可能的话,任何人都可以为我提供代码
提前致谢
更新:
没有错误出现,但在客户端,即 C# Side
来自服务器的 Message 的输出是 ?????
.
要将字节数组从 Server
发送到 Client
即 buffer
只需对 WriteFile
函数进行少量更改。
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
此方法会将整个 Byte Array
发送到 Client
并更改 buffer
int _count = 0;
UINT8 _imageBuffer[110592];
for (int _imageRow = 0; _imageRow < _image.rows; _imageRow++)
{
for (int _imageCol = 0; _imageCol < _image.cols; _imageCol++)
{
buffer[_count] = image.at<uchar>(_imageRow, _imageCol);
_count++;
}
}
我对缓冲区数组进行了硬编码,因为我知道我的相机只会发送 110592
字节来创建一帧。
并且在客户端只需使用 Read
函数。
int _imageRowSize = 288;
int _imageColSize = 384;
int _count = 0;
byte[] buffer = new byte[_imageColSize * _imageRowSize];
Image<Gray, UInt16> image = new Image<Gray, UInt16>(_imageColSize,_imageRowSize);
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
_count = 0;
int read = pipe.Read(buffer, 0, buffer.Length);
for (int _imageRow = 0; _imageRow < 288; _imageRow++)
{
for (int _imageCol = 0; _imageCol < 384; _imageCol++)
{
try
{
image.Data[_imageRow, _imageCol, 0] = (UInt16)(buffer[_count] * 255);
}catch(Exception exception)
{
Console.WriteLine(exception);
}
_count++;
}
}
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
}
CvInvoke.Imshow("Image", image);
}
我是 IPC
的菜鸟。我正在尝试将图像帧从我的 C++ Server
发送到 C# Client
。我已经开始学习它并制作了一个小的 Client
和 Server
,我的 C++ Server
在其中发送 Hello
。我看到了一个相关的问题,有人告诉我先将 Image
转换为 Byte Array
,然后以与 Hello Message
相同的方式发送,但我无法做到这一点。
我的基本客户端服务器代码
C++代码:
Mat image = imread("IMG_0_10_34_45_2018_1.bmp");
uchar buffer[500][500];
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
buffer[i][j] = image.at<unsigned char>(i, j);
}
}
cout << "Server Creating Pipe\n";
HANDLE hPipe = ::CreateNamedPipe(_T("\\.\pipe\HyperPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
0,
NULL);
cout << "Server Created Succesfully";
ConnectNamedPipe(hPipe, NULL);
cout << "Sending Message to Client";
DWORD bytesWritten = 0;
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
CloseHandle(hPipe);
return 0;
和C#代码:
static void Main(string[] args)
{
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
{
System.Console.WriteLine("Message from Server: " + rdr.ReadToEnd());
}
Console.ReadKey();
}
我还注意到,在我的 C++ Server
中,我必须更改 PIPE_TYPE to BYTE
和 READMODE to BYTE
。我将 OpenCV library
用于 Image Processing
,因此我可以轻松地使 Byte Array
没有问题。
所以,任何人都可以告诉我如何将 Byte Array
从 C++
发送到 C#
。
或者如果可能的话,任何人都可以为我提供代码
提前致谢
更新:
没有错误出现,但在客户端,即 C# Side
来自服务器的 Message 的输出是 ?????
.
要将字节数组从 Server
发送到 Client
即 buffer
只需对 WriteFile
函数进行少量更改。
WriteFile(hPipe, buffer, sizeof(buffer) * sizeof(uchar), &bytesWritten, NULL);
此方法会将整个 Byte Array
发送到 Client
并更改 buffer
int _count = 0;
UINT8 _imageBuffer[110592];
for (int _imageRow = 0; _imageRow < _image.rows; _imageRow++)
{
for (int _imageCol = 0; _imageCol < _image.cols; _imageCol++)
{
buffer[_count] = image.at<uchar>(_imageRow, _imageCol);
_count++;
}
}
我对缓冲区数组进行了硬编码,因为我知道我的相机只会发送 110592
字节来创建一帧。
并且在客户端只需使用 Read
函数。
int _imageRowSize = 288;
int _imageColSize = 384;
int _count = 0;
byte[] buffer = new byte[_imageColSize * _imageRowSize];
Image<Gray, UInt16> image = new Image<Gray, UInt16>(_imageColSize,_imageRowSize);
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
_count = 0;
int read = pipe.Read(buffer, 0, buffer.Length);
for (int _imageRow = 0; _imageRow < 288; _imageRow++)
{
for (int _imageCol = 0; _imageCol < 384; _imageCol++)
{
try
{
image.Data[_imageRow, _imageCol, 0] = (UInt16)(buffer[_count] * 255);
}catch(Exception exception)
{
Console.WriteLine(exception);
}
_count++;
}
}
if (read <= 0)
break;
ms.Write(buffer, 0, read);
}
}
CvInvoke.Imshow("Image", image);
}