如何在C#客户端和Java Server之间传输图像? [英] How do I transmit images between a C# client and a Java Server?

查看:64
本文介绍了如何在C#客户端和Java Server之间传输图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含C#客户端和Java(SE)服务器的项目,使用socket(tcp)进行通信。当客户端发送字符串或整数信息时,它工作正常,服务器可以完美地收到确切的信息。但是,我现在无法将图像从客户端发送到服务器或以可逆方式发送。我想出了一种可能的方法,将图像传输到字节数组并通过套接字发送字节数组,服务器收到后,服务器再次将数组传输到图像。这听起来可行,但在我实现和调试之后,部分字节数组被损坏了。请注意,C#-byte的范围是[0,255]而Java字节的范围是[-128,127]。因此,当一个大于128的C#字节传输到Java端时,它就会被损坏。

请帮助我,如果你提供一种传输图像的方法,它会受到关注在C#和Java之间。

I'm working on a project that contains a C# client and a Java(SE) server, using socket (tcp) for communication. When the client send string or integer information, it work just fine, the server can perfectly recieve the exact information. However, I'm now in trouble sending images from the client to the server or in the reversive way. I came up with a probable way by transferring the image to a byte array and send the byte array through socket and after the server recieves it, the server transfers the array to an image again. It sounds doable but after I implemented and debugged this, part of the byte array was "damaged". Note that a C#-byte's range is [0,255] while a Java-byte's range is [-128,127]. So when a C#-byte that is greater than 128 is transmitted to the Java side, it is "damaged".
Please help me out, it'll be apprieciated if you provide a way to transmit images between C# and Java.

推荐答案

iirc C#字节是无符号的 - 而C#sbyte IS签名ergo [-127,127]所以这将等同于java的字节..所以说,建议您可以使用该配对通过网络发送图像数据充满危险



如果不了解成像编码/解码,我发现它'奇怪'你会期望负字节



如果是我,我会考虑在c#和java方面进行抽象,以便它们可以传输数据并且每一端都可以处理编码/解码的问题(而不是'原始tcp套接字') - 那里有消息框架可以极大地帮助你
iirc C# byte is unsigned - whereas C# sbyte IS signed ergo [-127,127] so that would be equivalent to java's byte .. so saying, suggesting that you can use that pairing to send image data over the network is fraught with danger

Without know much about imaging encoding/decoding, I find it 'odd' that you would expect negative bytes

If it were me, I'd be looking at putting an abstraction over the c# and java sides, such that they can transmit the data and each end can handle the issues of encoding/decoding (as opposed to 'raw tcp sockets') - there are messaging frameworks out there that would greatly assist you


你可以尝试编码你的字节数组为Base64。这样所有值都应该在0到127之间。然后在Java端将它解码回字节数组。



Convert.ToBase64String Method(Byte []) [ ^ ]



Java Class Base64 [ ^ ]



这种方法会给你带来一些开销。
You could try to encode your byte array as Base64. That way all values should be between 0 - 127. Then decode it on the Java side back to a byte array.

Convert.ToBase64String Method (Byte[])[^]

Java Class Base64[^]

This approach will give you a bit of an overhead.


我想我刚刚找到了解决方案。总体思路与我在问题中所说的相同。

大约2个小时前,我奇怪地将同一个图像转移到C#和Java中的字节数组。当我查看字节数据时,我发现字节数据的[0,127]部分没有损坏。但是,如果C#数组中的元素大于127,例如C#byte:137,10001001,Java数组中的相应元素将是它的二进制补码,例如: Java字节:-119,01110111。我认为这是因为C#和Java之间的编码和解码方式不同,我不太确定。但这一切只是关于位操作,所以我为什么要关心这个呢?我需要做的就是将字节数组从一侧发送到另一侧。

以下是主要方法:

C#:
I think I just found the solution. The general idea was the same with what I had said in the question.
About 2 hours earlier, I curiously transferred the very same image to a byte array in C# as well as Java. When I looked into the byte data I found that the [0,127] part of byte data was not "damaged". However, if an element in the C# array is greater than 127, e.g. C# byte: 137, 10001001, the corresponding element in the Java array would be its two's complement, e.g. Java byte: -119, 01110111. I think this is because the encoding and decoding way is different between C# and Java, I'm not quite sure. But all this is just about bit operation, so why do I care about this? All I need to do is to send the byte array from one side to another.
Here is the main methods:
C#:
public static byte[] ImageToBytes(string path)
        {
            FileInfo fileInfo = new FileInfo(path);
            byte[] buffer = new byte[fileInfo.Length];
            using (FileStream stream = fileInfo.OpenRead())
            {
                stream.Read(buffer, 0, buffer.Length);
            }
            return buffer;
        }




public static bool sendBytes(byte[] content)
        {
            try
            {
                int length = content.Length;
                sendCode(length);                
                clientSocket.Send(content);
                return true;
            }
            catch
            {
                return false;
            }
        }





Java:



Java:

public static void byte2image(byte[] data, String path) {
		if (data.length < 3 || path.equals(""))
			return;
		try {
			FileImageOutputStream imageOutput = new FileImageOutputStream(
					new File(path));
			imageOutput.write(data, 0, data.length);
			imageOutput.close();
			System.out.println("Make Picture success,Please find image in "
					+ path);
		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
		}
	}




public static byte[] readBytes(){
		DataInputStream din=null;
		int length=readCode();
		byte[] b=new byte[length];
		try {
			din = new DataInputStream(socket.getInputStream());
			din.read(b);
			din.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return b;
	}


这篇关于如何在C#客户端和Java Server之间传输图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆