• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encoding a Java image  RSS feed

 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this is the correct forum for this, but I figured I would give it a shot.

If I have a fixed panel of 250 x 70 pixels, and the User is allowed to scribble all over it...what do people think is the most efficient way to store what was drawn to be sent over some network connection?
The problems I have been thinking about it that a bitmap might be nice, but that is always a fixed size no matter how much scribbling was done, and something that large, MOST OF THE TIME, would probably not be necessary. But having something a fixed size would also allow me to have a predictable amount of network traffic for every image, which other methods may be worse in the worst-case scenario (where every pixel is filled in).
Anyway, any help on this matter would be appreciated!
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most efficient method is a PNG encoder set to compression level=9.

A good freebee is Keypoints PngEncoder class. It is simple to use, i.e.:

PngEncoder encoder = new PngEncoder();
encoder.setCompressionLevel(9);
encoder.setEncodeAlpha(false);
encoder.setFilter(PngEncoder.FILTER_NONE);
encoder.setImage(<Image class> ;
byte[] result = encoder.pngEncode();

This will squash a 640x480 pixel image down to around 30k without quality loss. Then you can just encapsulate the byte array in something and send it.

At the other end just use standard Java ImageIO to get back the Image class.

If you want to stream the image, then consider creating a difference factory to pick out tiles of changed pieces of the image and send only them. This is only really of benefit for larger drawing area.In your case, I suspect you can just send all the image again.
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much John, I'll give that a shot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!