What are the options for serializing a BufferedImage?

Q

Qu0ll

BufferedImage does not appear to implement Serializable so I don't know how
to send an instance of this class from a server to an applet. The server
will not necessarily have the image file stored locally so I cannot use
ImageIO from the applet as the server will acquire the image from yet
another server. Ideally I would like to transmit the image as a Base64
encoded section in an XML file.

So what are the options for transmitting a BufferedImage? How do I encode
it into Base64?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
C

charlesbos73

BufferedImage does not appear to implement Serializable so I don't know how
to send an instance of this class from a server to an applet. The server

Indeed, but you can trivially access the image type and its underlying
int[] of a BufferedImage. And with these infos, you can trivially
reconstruct a BufferedImage at the other end.

Simply create a class (say SerializablePicture) that implements
the Serializable interface which contains the int[] and the image
type.

The following should work:

(btw Lew the JLS-nazi bot can go f*ck himself about my 4-spaces
indentation, I'm here to help people, not to nitpick)


public final class SerializablePicture implements Serializable {

static final long serialVersionUID = 0x17E30096AFA13BE7L;

private int w;
private int h;
private int imageType;
private int[] pixels;

public SerializablePicture(
final int w,
final int h,
final int imageType,
final int[] pixels
) {
this.w = w;
this.h = h;
this.imageType = imageType;
this.pixels = pixels;
}

public int getW() {
return w;
}

public int getH() {
return h;
}

public int getImageType() {
return imageType;
}

public int[] getPixels() {
return pixels;
}

}
will not necessarily have the image file stored locally so I cannot use
ImageIO from the applet as the server will acquire the image from yet
another server. Ideally I would like to transmit the image as a Base64
encoded section in an XML file.

That's a terrible misuse of XML in my opinion.

So what are the options for transmitting a BufferedImage? How do I encode
it into Base64?

Base64Encoder/Decoder would do, but I really wouldn't use that
to transmit a BufferedImage.
 
J

Joshua Cranmer

Qu0ll said:
So what are the options for transmitting a BufferedImage? How do I
encode it into Base64?

Save the image to a temporary file--or a generic OutputStream. ImageIO
has this functionality, and should be rather easy to use (PNG is
probably your best bet for transfer: lossless and better compression
than BMP).
 
Q

Qu0ll

charlesbos73 said:
BufferedImage does not appear to implement Serializable so I don't know
how
to send an instance of this class from a server to an applet. The server

Indeed, but you can trivially access the image type and its underlying
int[] of a BufferedImage. And with these infos, you can trivially
reconstruct a BufferedImage at the other end.

Simply create a class (say SerializablePicture) that implements
the Serializable interface which contains the int[] and the image
type.

The following should work:

(btw Lew the JLS-nazi bot can go f*ck himself about my 4-spaces
indentation, I'm here to help people, not to nitpick)


public final class SerializablePicture implements Serializable {

static final long serialVersionUID = 0x17E30096AFA13BE7L;

private int w;
private int h;
private int imageType;
private int[] pixels;

public SerializablePicture(
final int w,
final int h,
final int imageType,
final int[] pixels
) {
this.w = w;
this.h = h;
this.imageType = imageType;
this.pixels = pixels;
}

public int getW() {
return w;
}

public int getH() {
return h;
}

public int getImageType() {
return imageType;
}

public int[] getPixels() {
return pixels;
}

}
will not necessarily have the image file stored locally so I cannot use
ImageIO from the applet as the server will acquire the image from yet
another server. Ideally I would like to transmit the image as a Base64
encoded section in an XML file.

That's a terrible misuse of XML in my opinion.

So what are the options for transmitting a BufferedImage? How do I
encode
it into Base64?

Base64Encoder/Decoder would do, but I really wouldn't use that
to transmit a BufferedImage.

Thanks for the advice - I may well do this.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

Matt Humphrey said:
Here's a reference to some sample code. In a servlet they create a
BufferedImage, draw stuff to it and then direct it to the servlet output
via a JPEG encoder. Note the servlet response content type is set to
image/jpg.

http://javahowto.blogspot.com/2008/04/sample-servlet-that-dynamically-creates.html

Thank you Matt - that looks like the way to go and is probably what I will
use.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

Joshua Cranmer said:
Save the image to a temporary file--or a generic OutputStream. ImageIO has
this functionality, and should be rather easy to use (PNG is probably your
best bet for transfer: lossless and better compression than BMP).

Thanks Joshua, I considered this option but decided that I didn't want to
have an intermediate step that involved writing something to disk. It may
not be that time consuming but I think Matt's solution is more suitable.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

Qu0ll said:
Thank you Matt - that looks like the way to go and is probably what I will
use.

The only problem is that there appear to be no codecs for PNG or GIF - is
that correct? How would I handle those file types if required?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
Q

Qu0ll

Matt Humphrey said:
Qu0ll said:
The only problem is that there appear to be no codecs for PNG or GIF - is
that correct? How would I handle those file types if required?

I'm using Java 1.6_04 under Eclipse 3.3.1 and it reports the following
built-in codecs:
jpg, BMP, bmp, JPG, jpeg, wbmp, png, JPEG, PNG, WBMP, GIF, gif

Java Image / IO docs are here
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/imageio_guideTOC.fm.html

Try this code to see what codecs are available on your system:

public static final void main (String [] args) {
try {
String [] formatNames = ImageIO.getWriterFormatNames();
if (formatNames.length == 0) {
System.out.println("No output formats available.");
return;
}
System.out.print ("Available formats: ");

for (int i = 0; i < formatNames.length; ++i) {
if (i > 0) System.out.print (", ");
System.out.print (formatNames);
}
System.out.println ("");

BufferedImage image = new BufferedImage(300, 300,
BufferedImage.TYPE_BYTE_INDEXED);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.blue);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
graphics.setColor(Color.green);
graphics.fillRect(20, 60, 120, 170);

Iterator<ImageWriter> iws = ImageIO.getImageWritersByFormatName
("png");
if (! iws.hasNext()) {
System.out.println ("No available image writer");
}

ImageWriter iw = iws.next();

FileOutputStream fos = new FileOutputStream ("image1.png");
MemoryCacheImageOutputStream mos = new MemoryCacheImageOutputStream
(fos);
iw.setOutput(mos);
iw.write(image);
mos.close ();

} catch (Exception ex) {
ex.printStackTrace ();
}
System.out.println ("Done");
}


There are plenty of 3rd party encoders--there's probably a free one out
there GIYF.


Ah, thanks for that. I get the same list of codecs on Java 6 Update 14. I
was confused by the fact that there is a JPEGCodec class but no PNGCodec
class in ImageIO.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
J

Joshua Cranmer

R

Roedy Green

BufferedImage does not appear to implement Serializable so I don't know how
to send an instance of this class from a server to an applet.

turn it into a JPG PNG etc.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Never discourage anyone... who continually makes progress, no matter how slow.
~ Plato 428 BC died: 348 BC at age: 80
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top