能够接收width和height

This commit is contained in:
uu
2025-06-11 12:04:54 +08:00
parent c7825db6c8
commit 12eae7c1ac
10 changed files with 77 additions and 17 deletions

View File

@@ -59,8 +59,9 @@ namespace HitBotCSharpDemo
private bool isDetectionRunning = false;
private const int SERVER_PORT = 8888; // Python服务器端口
private const string SERVER_IP = "192.168.124.4"; // 本机IP
private float receivedWidth = 0;
private float receivedHeight = 0;
private CameraManager camManager;
private ControlBeanEx robot;
public ShowForm()
{
@@ -1067,7 +1068,7 @@ namespace HitBotCSharpDemo
try
{
string fileName = "temp_detection_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
string detectionFolder = Path.Combine(Application.StartupPath, "DetectionImages");
string detectionFolder = Path.Combine(Application.StartupPath, "DetectionImage");
string fullPath = Path.Combine(detectionFolder, fileName);
if (!Directory.Exists(detectionFolder))
@@ -1125,32 +1126,44 @@ namespace HitBotCSharpDemo
{
// 接收图像大小
byte[] sizeBuffer = new byte[4];
int bytesRead = 0;
while (bytesRead < 4)
{
int read = networkStream.Read(sizeBuffer, bytesRead, 4 - bytesRead);
if (read == 0) throw new Exception("连接断开");
bytesRead += read;
}
int bytesRead = ReceiveExactly(sizeBuffer, 4);
if (bytesRead != 4) throw new Exception("接收图像大小失败");
int imageSize = (sizeBuffer[0] << 24) | (sizeBuffer[1] << 16) |
(sizeBuffer[2] << 8) | sizeBuffer[3];
//MessageBox.Show($"准备接收处理结果图像,大小: {imageSize} 字节");
// 接收width
byte[] widthBuffer = new byte[4];
bytesRead = ReceiveExactly(widthBuffer, 4);
if (bytesRead != 4) throw new Exception("接收width失败");
// 将字节数组转换为浮点数(大端序)
if (BitConverter.IsLittleEndian)
Array.Reverse(widthBuffer);
receivedWidth = BitConverter.ToSingle(widthBuffer, 0);
// 接收height
byte[] heightBuffer = new byte[4];
bytesRead = ReceiveExactly(heightBuffer, 4);
if (bytesRead != 4) throw new Exception("接收height失败");
// 将字节数组转换为浮点数(大端序)
if (BitConverter.IsLittleEndian)
Array.Reverse(heightBuffer);
receivedHeight = BitConverter.ToSingle(heightBuffer, 0);
MessageBox.Show($"width:{receivedWidth}, height:{receivedHeight}");
// 接收图像数据
byte[] imageBuffer = new byte[imageSize];
bytesRead = 0;
while (bytesRead < imageSize)
{
int read = networkStream.Read(imageBuffer, bytesRead, imageSize - bytesRead);
if (read == 0) throw new Exception("连接断开");
bytesRead += read;
}
bytesRead = ReceiveExactly(imageBuffer, imageSize);
if (bytesRead != imageSize) throw new Exception("接收图像数据不完整");
// 保存接收到的图像
string receivedImagePath = Path.Combine(Application.StartupPath,
string startImagePath = Path.Combine(Application.StartupPath, "DetectionImage");
string receivedImagePath = Path.Combine(startImagePath,
"received_detection_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg");
File.WriteAllBytes(receivedImagePath, imageBuffer);
@@ -1161,6 +1174,20 @@ namespace HitBotCSharpDemo
throw new Exception("接收图像失败: " + ex.Message);
}
}
// 辅助方法:确保接收指定字节数的数据
private int ReceiveExactly(byte[] buffer, int size)
{
int totalBytesRead = 0;
while (totalBytesRead < size)
{
int bytesRead = networkStream.Read(buffer, totalBytesRead, size - totalBytesRead);
if (bytesRead == 0)
throw new Exception("连接断开");
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
// 在pictureBox1中显示检测结果
private void DisplayDetectionResult(string imagePath)
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@@ -204,3 +204,36 @@
2025-06-11 10:28:48.993 set_first_position_after_initial
2025-06-11 10:28:48.993 movej_old start_pos: -44.911701 98.662003 -75.475304 -1005.720947 end_pos: -44.911701 98.662003 -75.475304 -1005.720886 org_sp 10.000000 end_sp 10.000000
2025-06-11 10:28:49.275 J3 Belt Meilage=40.900181km
2025-06-11 10:43:33.695 SDK_VERSION_V2.0.0.29_Release
2025-06-11 10:43:33.695 robot connected
2025-06-11 10:43:33.696 26
2025-06-11 10:43:33.696 current generation=26
2025-06-11 10:43:34.199 0x1a
2025-06-11 10:43:35.053 initial joint2 2873744
2025-06-11 10:43:35.053 C:\Users\fusy_\source\repos\HitBotCSharpDemo_x64\HitBotCSharpDemo\HitBotCSharpDemo\bin\x64\Debug\\j4_pid.txt do not exist
2025-06-11 10:43:35.053 robot WritePID
2025-06-11 10:43:35.369 initial joint1 -654075
2025-06-11 10:43:35.420 initial joint2 2873743
2025-06-11 10:43:35.456 initial joint3 -219842
2025-06-11 10:43:35.502 initial joint4 -24758200
2025-06-11 10:43:35.519 initial joint1 -654075
2025-06-11 10:43:35.568 initial joint2 2873745
2025-06-11 10:43:35.602 initial joint3 -219843
2025-06-11 10:43:35.653 initial joint4 -24758200
2025-06-11 10:43:35.672 initial joint1 -654074
2025-06-11 10:43:35.738 initial joint2 2873745
2025-06-11 10:43:35.785 initial joint3 -219845
2025-06-11 10:43:35.833 initial joint4 -24758201
2025-06-11 10:43:37.996 initial_thread initialized
2025-06-11 10:43:37.996 servo enable
2025-06-11 10:43:37.996 brake open
2025-06-11 10:43:37.996 set_brake_state 0 1
2025-06-11 10:43:38.209 robot initialized
2025-06-11 10:43:38.775 get_scara_param -44.911598 98.662201 -75.478798 -1005.721008
2025-06-11 10:43:38.775 get_scara_real_coor -44.911598 98.662300 -75.479500 -1005.720825
2025-06-11 10:43:38.776 <09><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2><EFBFBD><EFBFBD>
2025-06-11 10:43:38.776 position -654072.562500 2873745.000000 -219847.937500 -24758206.000000 0.000000 0.000000 0.000000 0.000000
2025-06-11 10:43:38.776 speed 44911.601563 98662.203125 75478.796875 1020006.687500
2025-06-11 10:43:38.776 set_first_position_after_initial
2025-06-11 10:43:38.777 movej_old start_pos: -44.911598 98.662201 -75.478798 -1005.720947 end_pos: -44.911499 98.662201 -75.478798 -1005.720886 org_sp 10.000000 end_sp 0.938537
2025-06-11 10:43:39.076 J3 Belt Meilage=40.900356km