Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Images over java?  (Read 1991 times)
0 Members and 1 Guest are viewing this topic.
Offline blah42

Innocent Bystander





« Posted 2013-02-19 05: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?
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-02-19 05:36:51 »

What does "images over java" mean? Do you mean sending images over the internet using Java?

Offline Dane

Senior Newbie





« Reply #2 - Posted 2013-02-19 06: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();

// 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:
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!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-02-19 08: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.

Offline ReBirth
« Reply #4 - Posted 2013-02-19 10:40:44 »

But I think it's better to have all the contents on user side (client update).

Offline Dane

Senior Newbie





« Reply #5 - Posted 2013-02-19 15: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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2013-02-19 17: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.
Offline jonjava
« Reply #7 - Posted 2013-02-19 20: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.

Quote
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

Offline Dane

Senior Newbie





« Reply #8 - Posted 2013-02-19 21:22:11 »

An integer is a primitive data type that is made up of 32 bits. (4 bytes)   Shocked

Doing it wrong since 0x7CC.
Offline jonjava
« Reply #9 - Posted 2013-02-19 21:23:49 »

(╯°□°)╯︵ ┻━┻

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #10 - Posted 2013-02-19 21: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.


Offline jonjava
« Reply #11 - Posted 2013-02-19 21: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.

Offline Dane

Senior Newbie





« Reply #12 - Posted 2013-02-19 21:35:41 »

I know what forms data can take.  Roll Eyes

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.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #13 - Posted 2013-02-19 21:50:22 »

I know what forms data can take.  Roll Eyes

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.  Smiley

Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #14 - Posted 2013-02-20 06: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.
Offline HeroesGraveDev

JGO Kernel


Medals: 325
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #15 - Posted 2013-02-20 07: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().

Offline ReBirth
« Reply #16 - Posted 2013-02-20 10:56:19 »

Doesn't kryonet have casting method?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (13 views)
2014-12-29 04:39:27

CopyableCougar4 (17 views)
2014-12-28 20:10:29

BurntPizza (21 views)
2014-12-28 16:38:51

Mr.CodeIt (14 views)
2014-12-27 22:03:04

TheDudeFromCI (19 views)
2014-12-27 20:14:49

Mr.CodeIt (26 views)
2014-12-23 21:34:11

rwatson462 (58 views)
2014-12-16 03:26:44

Mr.CodeIt (47 views)
2014-12-15 13:50:38

BurntPizza (94 views)
2014-12-10 16:41:13

BurntPizza (115 views)
2014-12-08 22:46:31
How do I start Java Game Development?
by gouessej
2014-12-28 13:41:21

Resources for WIP games
by kpars
2014-12-19 04:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-10 16:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-03 16:36:02

List of Learning Resources
by Longor1996
2014-08-17 04:40:00

List of Learning Resources
by SilverTiger
2014-08-06 13:33:27

Resources for WIP games
by CogWheelz
2014-08-02 10:20:17

Resources for WIP games
by CogWheelz
2014-08-02 10:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!