我在 C# 中将图像从 android 发送到 TCPClient,但是当接收图像时,这是无效图像。
我创建了一个方法来验证流但没有图像头。
任何人都可以帮助我吗?
这是我的ANDROID代码: 尝试{
Socket sockTcp = new Socket(EnderecoSoftclinic, PORTATCP);
while(sockTcp.isClosed()){
Toast.makeText(contexto, "Tentando conectar ao SoftClinic", Toast.LENGTH_SHORT).show();
sockTcp.connect(sockTcp.getRemoteSocketAddress(), PORTATCP);
}
OutputStream outputStream = sockTcp.getOutputStream();
outputStream.write(imagem,0,imagem.length);
outputStream.flush();
sockTcp.close();
} catch (IOException e) {
e.printStackTrace();
}
我收到 C# 代码:
TcpListener tcpReceiver = new TcpListener(IPAddress.Any, 2553);
tcpReceiver.Start(1);
while (!finalizado)
{
TcpClient android = tcpReceiver.AcceptTcpClient();
NetworkStream stream = android.GetStream();
byte[] buffer = null;
buffer = new byte[Convert.ToInt32(tamanho)];
if (stream.CanRead)
{
while (stream.Read(buffer, 0, buffer.Length) > 0) {}
}
if (buffer == null || buffer.Length == 0)
{ return; }
MemoryStream streamImg = new MemoryStream();
streamImg.Write(buffer, 0, buffer.Length);
if (!IsValidImage(streamImg))
{
finalizado = true;
return;
}
if (File.Exists("C:/teste.bmp"))
{
File.Delete("C:/teste.bmp");
}
FileStream streamWriter = new FileStream("C:/teste.bmp", FileMode.CreateNew, FileAccess.Write);
streamWriter.Write(buffer, 0, buffer.Length);
streamWriter.Flush();
streamWriter.Close();
try
{
Image imagemGerada = Image.FromStream(streamImg);
if (AoRetornarDadosEquipamento != null)
{
AoRetornarDadosEquipamento(imagemGerada);
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
stream.Close();
android.Close();
finalizado = true;
}
tcpReceiver.Stop();