I am making a 2d networked game and currently am sending integers through my server successfully, But i would like to enable a system that allows custom skins, so I would like to know if there is a way to send images over java using the default files, if so could I have an example of how this is done?
What does “images over java” mean? Do you mean sending images over the internet using Java?
Here’s an idea:
(Assuming the image is a BufferedImage)
Writing:
int[] data = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
// Write the length of the image.
net.writeInteger( data.length );
net.writeShort( img.getWidth() );
net.writeShort( img.getHeight() );
// Write the image's data.
for (int i : data) {
net.writeInteger(i);
}
Reading:
int available = net.readInteger();
int width = net.readShort();
int height = net.readShort();
BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
for (int i = 0; i < available; i++)
{
data[i] = net.readInteger();
}
There is no spoon image. There are no integers or files or music files or whatever.
If you can send bytes from A to B you can send anything.
But I think it’s better to have all the contents on user side (client update).
If you create a BufferedImage with the type INT_RGB/ARGB then the data is an integer, like a white pixel would be 0xFFFFFF.
Plus an integer is 4 bytes, so I don’t see how that wouldn’t be applicable.
I hear there’s this great internet transport for all kinds of media types. In fact you’re using it to read this post.
Hmm, what? o.O
Data is quite abstract in many cases. However, in computers, data is made up of bytes.
Plus an integer is 4 bytes, so I don’t see how that wouldn’t be applicable.
I don’t understand what you mean :V
An integer is a primitive data type that is made up of 32 bits. (4 bytes) :o
(╯°□°)╯︵ ┻━┻
I don’t think you understand. Data never take any form, other than bytes. You decide how to interpret these bytes though.
You can send the bytes of an image over the network, and interpret those bytes as an image when you recieve them.
Example:
I can send 32 bits over the network, in a packet. When I receive them, I can interpret them as an integer, representing one huge number.
However, I can also interpret them as a coordinate set composed of two shorts (16 bit each). Yet again, I can interpret them as four letters (4 bits per letter).
Then again, I can interpret them as three letters, and a number between -127 and 127. It’s the same data I’m sending though.
This is what protocols are for. You have to decide how you interpret the data you’re sending over the network. It’s the same reason you can open any file-type with any software in Windows. It’s all the same data, but different software make different expectations to it, and interpret it a specific way.
What I’m trying to say is that sending images are made up of bytes. There’s no such thing that sends an “image” over a network. It’s all 1’s and 0’s.
So if you can already send integers (4 bytes) it means you’ve already solved the problem. Does that make sense?
Sorry for explaining poorly.
[EDIT]: I’m need to go to sleep.
I know what forms data can take. :
I was just giving an idea of what he could do; which is send the image’s pixels in the form of an integer and interpret them on the client as an integer.
Anyways, I don’t think sending images from the client to the server is a great idea unless they’re being cached/saved somewhere where they can be re-loaded, and if you plan on having those same images change then somehow a way to check if they’re the same version as the servers. (Like a CRC check or something.)

I know what forms data can take. :
I was just giving an idea of what he could do; which is send the image’s pixels in the form of an integer and interpret them on the client as an integer.
Anyways, I don’t think sending images from the client to the server is a great idea unless they’re being cached/saved somewhere where they can be re-loaded, and if you plan on having those same images change then somehow a way to check if they’re the same version as the servers. (Like a CRC check or something.)
Since this is in the context of gaming, I’ll agree with you. I can think of multiple instances where I’d like to send image information over a network with java though.
How about subclassing BufferedImage and implementing interface java.io.serializable?
You can then implement
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
Now look at java.io.ObjectOutputStream and java.io.ObjectInputStream etc.
I haven’t tried this. There’s probably some additional overhead too, but probably not much compared to the image data. It might be worth compressing the data before transmission too. It might be better to make a general purpose update utility, so you can update any file on load, not just the skins.
All these ideas are overcomplicating the question.
Use ImageIO.write() to write the image and ImageIO.read() to read it.
Edit:
And if the source of the image is a file (ie: not created in the application) then you can just copy the file’s input stream to the socket’s output stream and read it with ImageIO.read().
Doesn’t kryonet have casting method?