Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过套接字发送缓冲区映像_Java_Sockets_Serialization_Bytearray_Bufferedimage - Fatal编程技术网

Java 通过套接字发送缓冲区映像

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

我想通过网络发送一个缓冲映像,作为自定义类的一部分

我目前只需要writeObject和readObject来获取我的类

要发送我当前正在执行的图像,请执行以下操作:

((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线程中有答案。我添加了这个以获取帮助