Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 827 times)
0 Members and 1 Guest are viewing this topic.
Offline blah42

JGO Visitor




« 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?
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« 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?

Offline 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();

// 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

JGO Knight


Medals: 32



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

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

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

JGO Knight


Medals: 32



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

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 22: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

JGO Knight


Medals: 32



« Reply #9 - Posted 2013-02-19 22:23:49 »

(╯°□°)╯︵ ┻━┻

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Mads

JGO Ninja


Medals: 23
Projects: 3


One for all!


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


Offline jonjava

JGO Knight


Medals: 32



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

Offline Dane

Senior Newbie





« Reply #12 - Posted 2013-02-19 22: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: 23
Projects: 3


One for all!


« Reply #13 - Posted 2013-02-19 22: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: 7
Projects: 3


Java tames rock!


« 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.
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« 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().

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

Doesn't kryonet have casting method?

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
hobbles (8 views)
2013-05-22 00:54:55

cubemaster21 (75 views)
2013-05-17 21:29:12

alaslipknot (87 views)
2013-05-16 21:24:48

gouessej (116 views)
2013-05-16 00:53:38

gouessej (111 views)
2013-05-16 00:17:58

theagentd (122 views)
2013-05-15 15:01:13

theagentd (111 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (176 views)
2013-05-12 15:36:09
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.124 seconds with 21 queries.