Java 通过套接字发送缓冲区映像
我想通过网络发送一个缓冲映像,作为自定义类的一部分 我目前只需要writeObject和readObject来获取我的类 要发送我当前正在执行的图像,请执行以下操作:Java 通过套接字发送缓冲区映像,java,sockets,serialization,bytearray,bufferedimage,Java,Sockets,Serialization,Bytearray,Bufferedimage,我想通过网络发送一个缓冲映像,作为自定义类的一部分 我目前只需要writeObject和readObject来获取我的类 要发送我当前正在执行的图像,请执行以下操作: ((DataBufferByte)i.getData().getDataBuffer()).getData() 如何将其转换回BuffereImage 有没有更好的办法 我发送的类如下所示: public class imagePack{ public byte[] imageBytes; public String clien
((DataBufferByte)i.getData().getDataBuffer()).getData()代码>
如何将其转换回BuffereImage
有没有更好的办法
我发送的类如下所示:
public class imagePack{
public byte[] imageBytes;
public String clientName;
public imagePack(String name, BufferedImage i){
imageBytes = ((DataBufferByte) i.getData().getDataBuffer()).getData();
clientName = name;
}
public BufferedImage getImage(){
//Do something to return it}
}
再次感谢如果要将其转换回BuffereImage,还必须知道其宽度、高度和类型
class imagePack {
public byte[] imageBytes;
public int width, height, imageType;
public String clientName;
public imagePack(String name, BufferedImage i) {
imageBytes = ((DataBufferByte) i.getData().getDataBuffer())
.getData();
width = i.getWidth();
height = i.getHeight();
imageType = i.getType();
clientName = name;
}
public BufferedImage getImage() {
if (imageType == BufferedImage.TYPE_CUSTOM)
throw new RuntimeException("Failed to convert.");
BufferedImage i2 = new BufferedImage(width, height, imageType);
byte[] newImageBytes = ((DataBufferByte) i2.getData()
.getDataBuffer()).getData();
System.arraycopy(imageBytes, 0, newImageBytes, 0, imageBytes.length);
return i2;
}
}
“发送我当前正在做的图像:”这是一种低效的图像传输方式。首先将其编码为PNG或JPG。可能重复:我同意@Cratylus,但这对/@LmC似乎不太合适:我认为OP的问题是如何将DataBufferByte
转换为BuffereImage
。api提供了字节[]
,因此问题是如何将字节[]
转换为缓冲图像
,该图像在另一个so线程中有答案。我添加了这个以获取帮助