blah42
|
 |
«
Posted
2013-02-19 06:01:32 » |
|
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?
|
|
|
|
|
ra4king
|
 |
«
Reply #1 - Posted
2013-02-19 06:36:51 » |
|
What does "images over java" mean? Do you mean sending images over the internet using Java?
|
|
|
|
Dane
Senior Newbie 
|
 |
«
Reply #2 - Posted
2013-02-19 07:21:26 » |
|
Here's an idea: (Assuming the image is a BufferedImage) Writing: 1 2 3 4 5 6 7 8 9 10 11
| int[] data = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
net.writeInteger( data.length ); net.writeShort( img.getWidth() ); net.writeShort( img.getHeight() );
for (int i : data) { net.writeInteger(i); } |
Reading: 1 2 3 4 5 6 7 8 9 10 11
| 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(); } |
|
Doing it wrong since 0x7CC.
|
|
|
Games published by our own members! Check 'em out!
|
|
jonjava
|
 |
«
Reply #3 - Posted
2013-02-19 09:57:45 » |
|
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.
|
|
|
|
ReBirth
|
 |
«
Reply #4 - Posted
2013-02-19 11:40:44 » |
|
But I think it's better to have all the contents on user side (client update).
|
|
|
|
Dane
Senior Newbie 
|
 |
«
Reply #5 - Posted
2013-02-19 16:07:25 » |
|
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.
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.
|
Doing it wrong since 0x7CC.
|
|
|
sproingie
|
 |
«
Reply #6 - Posted
2013-02-19 18:09:04 » |
|
I hear there's this great internet transport for all kinds of media types. In fact you're using it to read this post.
|
|
|
|
|
jonjava
|
 |
«
Reply #7 - Posted
2013-02-19 21:16:18 » |
|
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.
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. 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
|
|
|
|
Dane
Senior Newbie 
|
 |
«
Reply #8 - Posted
2013-02-19 22:22:11 » |
|
An integer is a primitive data type that is made up of 32 bits. (4 bytes) 
|
Doing it wrong since 0x7CC.
|
|
|
jonjava
|
 |
«
Reply #9 - Posted
2013-02-19 22:23:49 » |
|
(╯°□°)╯︵ ┻━┻
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mads
|
 |
«
Reply #10 - Posted
2013-02-19 22:29:47 » |
|
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.
|
|
|
|
jonjava
|
 |
«
Reply #11 - Posted
2013-02-19 22:32:21 » |
|
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.
|
|
|
|
Dane
Senior Newbie 
|
 |
«
Reply #12 - Posted
2013-02-19 22:35:41 » |
|
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.)
|
Doing it wrong since 0x7CC.
|
|
|
Mads
|
 |
«
Reply #13 - Posted
2013-02-19 22:50:22 » |
|
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. 
|
|
|
|
Alan_W
|
 |
«
Reply #14 - Posted
2013-02-20 07:47:13 » |
|
How about subclassing BufferedImage and implementing interface java.io.serializable? You can then implement 1 2 3
| 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.
|
Time flies like a bird. Fruit flies like a banana.
|
|
|
HeroesGraveDev
|
 |
«
Reply #15 - Posted
2013-02-20 08:41:31 » |
|
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().
|
|
|
|
ReBirth
|
 |
«
Reply #16 - Posted
2013-02-20 11:56:19 » |
|
Doesn't kryonet have casting method?
|
|
|
|
|